使用smtp发送邮件时出错

使用smtp发送邮件时出错

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

问题描述

我的代码是:

My code is:

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

  String host = "";//or IP address
  final String user="";;//change accordingly
  final String password="";//change accordingly

  String to="";//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:, 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

推荐答案

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,



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

08-20 15:20