使用 java mail api 发送邮件时出现 MailConnectException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20766044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MailConnectException while sending mail using java mail api
提问by Susie
Trying to send an email using java mail api. And I keep getting MailConnectException. I have tried multiple ways to solve it without success.
尝试使用 java mail api 发送电子邮件。我不断收到 MailConnectException。我尝试了多种方法来解决它,但没有成功。
Exception is thrown by this statement
此语句抛出异常
transport.connect("smtp.gmail.com", "[email protected]", "myPassword");
Can anyone tell me what I'm doing wrong?
谁能告诉我我做错了什么?
public static void main(String[] args) {
String host = "smtp.gmail.com";
String from = "[email protected]";
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", "myPassword");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.auth", "true");
try{
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipients(Message.RecipientType.TO, "[email protected]");
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", "[email protected]", "myPassword");//CAUSES EXCEPTION
transport.sendMessage(message, message.getAllRecipients());
}catch(MessagingException e){
e.printStackTrace();
}
}
Stack trace:
堆栈跟踪:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1984)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at com.karmacrafts.util.CustomEmail.main(CustomEmail.java:127)
Caused by: java.net.ConnectException: Connection timed out: 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:301)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
... 4 more
采纳答案by Caadi0
This looks like network problem. Even though it could occur due to variety of reasons, like :-
这看起来像网络问题。即使它可能由于各种原因而发生,例如:-
"Couldn't connect to host, port" could be caused by wrong host name, wrong port, a blocking firewall (on the server, on gateways, even on your own machine), network failure, server downtime, etc.
“无法连接到主机、端口”可能是由错误的主机名、错误的端口、阻止防火墙(在服务器上、网关上,甚至在您自己的机器上)、网络故障、服务器停机等引起的。
Can you connect to the mail server using telnet ?
您可以使用 telnet 连接到邮件服务器吗?
Also see this FAQ for some mistakes you committed http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes
另请参阅此常见问题解答以了解您犯下的一些错误http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes
Read this answer on how to send emails using gmail https://stackoverflow.com/a/47452/3107043
阅读有关如何使用 gmail 发送电子邮件的答案https://stackoverflow.com/a/47452/3107043
回答by shiva raj
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
props.put("mail.smtp.port", "587"); transport.send(message);
回答by Congmin Qiu
I met the same problem, and the reason is that I opened a vpn. Hope this will be helpful.
我遇到了同样的问题,原因是我开了一个vpn。希望这会有所帮助。
回答by JeSa
After 8 hours of pain, I've solved this by turning off:
经过 8 小时的痛苦,我通过关闭解决了这个问题:
1) my windows firewall
1)我的windows防火墙
2) my antivirus software
2)我的杀毒软件
hope this helps
希望这可以帮助