[英]javax.mail.MessagingException: Could not connect to SMTP host?
following is my code to send mail: 以下是我发送邮件的代码:
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>");
}
}
but when i run the code it gives me the following exception. 但是当我运行代码时,它给了我以下异常。
javax.mail.MessagingException: Could not connect to SMTP host: usdc2spam2.slingmedia.com, port: 9000;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at samples.SendMail.sendMail(SendMail.java:46)
at samples.SendMail.main(SendMail.java:55)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
when i ping this usdc2spam2.slingmedia.com
it gives me reply without any problem. 当我ping这个usdc2spam2.slingmedia.com
它给我回复没有任何问题。 I am using windows 7
我正在使用windows 7
Please help me to resolve this. 请帮我解决这个问题。
This is these two lines which was casting me the problem : 这就是这两行给我带来的问题:
m_properties.put("mail.smtp.socketFactory.port", "465");
m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
and added this line : 并添加了这一行:
m_properties.put("mail.smtp.starttls.enable", "true");
After removing and adding the above lines of code it worked fine. 删除并添加上面的代码后,它工作正常。
What causes your problem is right there in the stack trace: 是什么导致您的问题就在堆栈跟踪中:
java.net.ConnectException: Connection refused: connect
do you need a password to connect to the SMTP server? 您需要密码才能连接到SMTP服务器吗? Are you sure you are using the right settings (as in port number)? 您确定使用的是正确的设置(如端口号)吗? Are you behind a proxy or a firewall? 你是代理人还是防火墙? Can you use those settings in a regular mail program (eg Thunderbird) and send mails? 您可以在常规邮件程序(例如Thunderbird)中使用这些设置并发送邮件吗?
尝试将端口9000添加到Windows防火墙中的入站规则中。
This exception usually occurs when there is no service listening on the port you are trying to connect to. 当您尝试连接的端口上没有服务侦听时,通常会发生此异常。
Try to connect using putty
or telnet
. 尝试使用putty
或telnet
连接。 I can bet you will get the same error. 我敢打赌你会得到同样的错误。
Verify these things: 验证这些事情:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.