使用smtp发送邮件时出错 [英] error in mail sending with smtp

查看:190
本文介绍了使用smtp发送邮件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是:

My code is:

public class SendMailBySite {
public static void main(String[] args) {

  String host = "host name";//or IP address
  final String user="user name";;//change accordingly
  final String password="XXXXXX";//change accordingly
  
  String to="mail addr";//change accordingly

   //Get the session object
   Properties props = new Properties();
   props.put("mail.smtp.host",host);
   props.put("mail.smtp.auth", "true");
   props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
   props.put("mail.smtp.socketFactory.fallback", "false");
   props.put("mail.smtp.socketFactory.port", "465");
   
   Session session = Session.getDefaultInstance(props,
    new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
	return new PasswordAuthentication(user,password);
      }
    });

   //Compose the message
    try {
     MimeMessage message = new MimeMessage(session);
     message.setFrom(new InternetAddress(user));
     message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
     message.setSubject("javatpoint");
     message.setText("This is simple program of sending email using JavaMail API");

    //send the message
     Transport.send(message);

     System.out.println("message sent successfully...");

     } catch (MessagingException e) {e.printStackTrace();}
 }
}



,错误是:


and error is:

javax.mail.MessagingException: Could not connect to SMTP host: host name, port: 25;
  nested exception is:
	java.net.ConnectException: Connection refused: connect
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
	at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
	at javax.mail.Service.connect(Service.java:313)
	at javax.mail.Service.connect(Service.java:172)
	at javax.mail.Service.connect(Service.java:121)
	at javax.mail.Transport.send0(Transport.java:190)
	at javax.mail.Transport.send(Transport.java:120)
	at com.SendMailBySite.main(SendMailBySite.java:42)
Caused by: java.net.ConnectException: Connection refused: connect
	at java.net.TwoStacksPlainSocketImpl.socketConnect(Native Method)
	at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
	at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
	at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
	at java.net.PlainSocketImpl.connect(Unknown Source)
	at java.net.SocksSocketImpl.connect(Unknown Source)
	at java.net.Socket.connect(Unknown Source)
	at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
	at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
	at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
	at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:201)
	at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1672)
	... 7 more

推荐答案

您好,



你可以试试用foll吗?欠代码。或者,如果您使用的是spring框架,那么您可以尝试使用 org.springframework.mail.javamail.JavaMailSenderImpl

Hello,

Can you try with following code. Alternatively if you are using spring framework, then you can try using org.springframework.mail.javamail.JavaMailSenderImpl.
public void doSend(MimeMessage msg, String strHost, int intPort, String strUser, String strPass) {
    String msgId = null;
    Session ses = null;
    Transport trans = null;
 
    try {
        ses = Session.getInstance(new Properties());
        trans = session.getTransport("smtp");
        trans.connect(strHost, intPort, strUser, strPass);
        if (null == msg.getSentDate()) msg.setSentDate(new Date());
        msgId = msg.getMessageID();
        msg.saveChanges();
        if (null == msgId) msg.setHeader(HEADER_MESSAGE_ID, msgId);
        trans.sendMessage(msg, msg.getAllRecipients());
    } catch(AuthenticationFailedException ex) {
        log.error("Unable to authenticate with the mail server!", ex);
    } catch (MessagingException ex) {
        log.error("Unexpected error occurred while sending the message!", ex);
    } finally {
        doCleanup(trans);
        if (ses != null) ses = null;
        if (trans != null) trans = null;
    }
}

private void doCleanup(Transport in) {
    try {
        if (in != null) in.close();
    } catch (MessagingException ex) {
        log.error("Unexpected error occurred while closing the transport!", ex);
    }
}



问候,


Regards,


请勿使用网络服务器进行测试。当你经常尝试或犯错时,他们会拒绝连接。



使用 HmailServer [ ^ ]进行测试。它是您机器的本地电子邮件服务器。请注意,这个也有禁止选项(关闭禁止功能是开发的首选)。
Do NOT use webservers for testing. They refuse connection when you attempt to often or make mistakes.

Use HmailServer[^] for testing. It's a local emailserver for your machine. Please be aware that this one has also a ban-option (switching off ban function is preferable for development).


这篇关于使用smtp发送邮件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆