简体   繁体   English

使用 java mail api 发送邮件时出现 MailConnectException

[英]MailConnectException while sending mail using java mail api

Trying to send an email using java mail api.尝试使用 java mail api 发送电子邮件。 And I keep getting MailConnectException.我不断收到 MailConnectException。 I have tried multiple ways to solve it without success.我尝试了多种方法来解决它,但没有成功。

Exception is thrown by this statement此语句抛出异常

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

Can anyone tell me what I'm doing wrong?谁能告诉我我做错了什么?

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

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

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

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

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希望这有帮助

I met the same problem, and the reason is that I opened a vpn.我遇到了同样的问题,原因是我开了一个vpn。 Hope this will be helpful.希望这会有所帮助。

For me, de-activating firewall and anti-virus did not work.对我来说,停用防火墙和防病毒软件不起作用。 I tried the alternate ports given in the mailtrap's SMTP settings 25 or 465 or 587 or 2525我尝试了邮件陷阱的 SMTP 设置 25 或 465 或 587 或 2525 中给出的备用端口

changing spring.mail.port to 2525 in the applications.properties file worked for meApplications.properties文件spring.mail.port更改为 2525 对我spring.mail.port

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM