使用java邮件api发送邮件时发生MailConnectException

时间:2013-12-24 19:42:14

标签: java web-applications web javamail registration

尝试使用java mail api发送电子邮件。而且我一直收到MailConnectException。我尝试过多种方法来解决它而没有成功。

此声明抛出异常

transport.connect("smtp.gmail.com", "someone@gmail.com", "myPassword");

谁能告诉我我做错了什么?

public static void main(String[] args) {
    String host = "smtp.gmail.com";
    String from = "someone@gmail.com";
    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, "someone@hotmail.com");
        message.setSubject("sending in a group");
        message.setText("Welcome to JavaMail");
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.gmail.com", "someone@gmail.com", "myPassword");//CAUSES EXCEPTION
        transport.sendMessage(message, message.getAllRecipients());
    }catch(MessagingException e){
        e.printStackTrace();
    }
}

堆栈跟踪:

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

5 个答案:

答案 0 :(得分:4)

这看起来像网络问题。即使它可能由于各种原因而发生,例如: -

“无法连接到主机,端口”可能是由错误的主机名,错误的端口,阻塞防火墙(在服务器上,网关上,甚至在您自己的计算机上)引起的,网络故障,服务器停机等

您可以使用telnet连接到邮件服务器吗?

另请参阅此常见问题解答,了解您提交的一些错误http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes

阅读有关如何使用Gmail https://stackoverflow.com/a/47452/3107043

发送电子邮件的答案

答案 1 :(得分:1)

经过8个小时的痛苦,我已通过关闭电源来解决此问题:

1)我的Windows防火墙

2)我的防病毒软件

希望这会有所帮助

答案 2 :(得分:0)

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);

答案 3 :(得分:0)

我遇到了同样的问题,原因是我打开了一个vpn。希望这会有所帮助。

答案 4 :(得分:0)

对我来说,停用防火墙和防病毒功能无效。 我尝试了邮件陷阱的SMTP设置中提供的备用端口 25或465或587或2525

applications.properties 文件中将spring.mail.port更改为2525对我有用