javax.mail.MessaginException:无法连接到SMTP主机?
以下是我发送邮件的代码:javax.mail.MessaginException:无法连接到SMTP主机?,java,email,servlets,Java,Email,Servlets,以下是我发送邮件的代码: import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Message.RecipientType; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public void sendMail(String m_from,String m_to,String m_subject,String m_body){
try {
Session m_Session;
Message m_simpleMessage;
InternetAddress m_fromAddress;
InternetAddress m_toAddress;
Properties m_properties;
m_properties = new Properties();
m_properties.put("mail.smtp.host", "usdc2spam2.slingmedia.com");
m_properties.put("mail.smtp.socketFactory.port", "465");
m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
m_properties.put("mail.smtp.auth", "true");
m_properties.put("mail.smtp.port", "9000");
m_Session=Session.getDefaultInstance(m_properties,new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("aaaaa","bbbbb@1"); // username and the password
}
});
m_simpleMessage = new MimeMessage(m_Session);
m_fromAddress = new InternetAddress(m_from);
m_toAddress = new InternetAddress(m_to);
m_simpleMessage.setFrom(m_fromAddress);
m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
m_simpleMessage.setSubject(m_subject);
m_simpleMessage.setContent(m_body, "text/html");
//m_simpleMessage.setContent(m_body,"text/plain");
Transport.send(m_simpleMessage);
} catch (MessagingException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
SendMail send_mail = new SendMail();
String empName = "xxxxx";
String title ="<b>Hi !"+empName+"</b>";
send_mail.sendMail("123erft@slingmedia.com", "abz@gmail.com", "Please apply for leave for the following dates", title+"<br>by<br><b>HR<b>");
}
}
当我点击这个usdc2spam2.slingmedia.com
时,它会毫无问题地给我回复。我正在使用Windows7
请帮助我解决此问题。导致问题的原因就在堆栈跟踪中:
java.net.ConnectException: Connection refused: connect
您需要密码才能连接到SMTP服务器吗?您确定使用了正确的设置(如端口号)?你是在代理还是防火墙后面?您能否在常规邮件程序(如Thunderbird)中使用这些设置并发送邮件?尝试将端口9000添加到windows防火墙的入站规则中。此异常通常发生在您尝试连接的端口上没有服务侦听时 尝试使用
putty
或telnet
进行连接。我敢打赌你也会犯同样的错误
验证以下内容:
- 您尝试连接的主机名和端口
- 服务器正在正确侦听,并且
- 没有防火墙阻止连接
m_properties.put("mail.smtp.socketFactory.port", "465");
m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
增加了这一行:
m_properties.put("mail.smtp.starttls.enable", "true");
删除并添加上述代码行后,效果良好。ping并不能证明您可以发送电子邮件。请尝试
telnet usdc2spam2.slingmedia.com 9000
。或者,如果您(Win7)没有telnet
,请使用例如当您尝试telnet usdc2spam2.slingmedia.com 9000
时会发生什么?我怀疑您在mail.smtp.port
属性中指定了错误的端口。您应该解释为什么不需要此行
m_properties.put("mail.smtp.starttls.enable", "true");