无法使用JavaMail连接到Hotmail

时间:2012-07-25 22:18:54

标签: java javamail hotmail

首先,如果这是一个重复的问题,请让我合并。在过去的几个日历月中,我一直在尝试使用JavaMail发送电子邮件;通过我的Hotmail帐户。我已经使用过去在此主题上发布到本网站的数字提示和代码片段;但是,我仍然得到一个java.net.ConnectException:连接被拒绝...当我执行Transport.connect方法时

这是我的代码

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演示代码中的代码,但stll会收到错误。
  • 该代码适用于雅虎,at&t,gmail和其他人......但不适用于Hotmail!

2 个答案:

答案 0 :(得分:2)

我删除了所有“socketFactory”内容,如Using javamail to send from 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;
  } 

答案 1 :(得分:0)

据推测,您已阅读this JavaMail FAQ entry

您是否也尝试过调试提示here

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