0

首先,如果这是一个重复的问题,让我申请一下。在过去的几个月里,我一直在尝试使用 JavaMail 发送电子邮件。通过我的 Hotmail 帐户。我已经使用了过去发布到该站点的有关此主题的大量提示和代码片段;但是,当我执行 Transport.connect 方法时,我仍然收到 java.net.ConnectException: Connection denied...

这是我的代码

String fromUserName = "testEmailAddress@hotmail.com";
String fromEMailAddress = "My Email Test <testEmailAddress@hotmail.com>";
String fromEmailPassword = "testEmailPassword";
String emailServerName = "smtp.live.com";
String emailServerPort = "587";
String toEMailAddress = "<TestDestinationEmailAddress@gmail.com>";      

Properties emailProps = System.getProperties();
emailProps.put("mail.smtps.host", emailServerName);
emailProps.put("mail.smtps.auth", "true");
emailProps.put("mail.transport.protocol", "smtps");
emailProps.put("mail.smtps.starttls.enable", "true");
emailProps.put("mail.smtps.ssl.enable","true");
emailProps.put("mail.smtps.port", emailServerPort);
emailProps.put("mail.debug", "true");

Authenticator localAuthenticator = new SMTPAuthenticator(fromUserName, fromEmailPassword);

Session emailSession = Session.getInstance(emailProps, localAuthenticator);

try {

    SMTPTransport emTransport = (SMTPTransport) emailSession.getTransport("smtps"); 
    emTransport.connect(emailServerName, Integer.parseInt(emailServerPort), fromUserName, fromEmailPassword);
    System.out.println("Ok, we connected ok.");

    MimeMessage emailMsg = new MimeMessage(emailSession);           
    emailMsg.addRecipient(Message.RecipientType.TO, new InternetAddress(toEMailAddress));
    emailMsg.setFrom(new InternetAddress(fromEMailAddress));
    emailMsg.setSubject("Automated Notification Number 1");
    emailMsg.setContent(getHtmlContent(), "text/html");
    emTransport.sendMessage(emailMsg, emailMsg.getAllRecipients());
    System.out.println("Sent Message#: 1");

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

这是一个例外......

DEBUG JavaMail version 1.4.5
DEBUG successfully loaded resource: /META-INF/javamail.default.providers
DEBUG SMTP useEhlo true, useAuth true
DEBUG SMTP trying to connect to host "smtp.live.com", port 587, isSSL true
javax.mail.MessagingException: Could not connect to SMTP host: smtp.live.com, port: 587;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
    at javax.mail.Service.connect(Service.java:295)
    at com.acf.TestingClasses.EmailSendingGames.sendMail(Unknown Source)
    at com.acf.TestingClasses.EmailSendingGames.main(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect



有几件事:

  • 我已经尝试过“smtp”和“smtps”......这似乎并不重要
  • 我尝试过端口:25、465、587 和 995...仍然拒绝连接
  • 我已经在许多计算机上尝试了相同结果的代码。
  • 从 JavaMail 演示代码中剪切并粘贴代码,但仍然会出现错误。
  • 该代码适用于 yahoo、at&t、gmail 和其他...但不适用于 Hotmail!

4

2 回答 2

2

我删除了所有“socketFactory”的东西,如使用 javamail 从 hotmail 发送? 然后它工作得很好......

下面是函数:

private Properties _setProperties() {   
    Properties props = new Properties();   

    props.put("mail.smtp.host", _host);   

    if(_debuggable) {   
      props.put("mail.debug", "true");   
    }   

    if(_auth) {   
      props.put("mail.smtp.auth", "true");   
    }

    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.host", _host);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.port", _port);

    return props;
  } 
于 2015-05-26T19:06:34.893 回答
0

想必您已经阅读过这个 JavaMail FAQ entry

您是否也尝试过这里的调试技巧?

从您发布的调试输出来看,您似乎有防火墙或阻止您连接到该站点的东西。

于 2012-07-25T22:41:40.213 回答