首先,如果这是一个重复的问题,让我来解释一下。在过去的几个月里,我一直在尝试使用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"one_answers"smtps"。。。这似乎无关紧要
- 我试过端口:25、465、587和995…仍然拒绝连接
- 我在许多计算机上试用过这些代码,结果都是一样的
- 剪切并粘贴了JavaMail演示代码中的代码,但stll收到了错误
- 该代码适用于雅虎的&t、 gmail和其他。。。但不是Hotmail!
我删除了所有的"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;
}
大概您已经阅读了这个JavaMail常见问题条目。
你也试过这里的调试技巧吗?
从你发布的调试输出来看,你似乎有一个防火墙或其他东西阻止你连接到该网站。