使用Gmail发送电子邮件时出错

时间:2014-10-15 23:38:21

标签: java javamail

我有以下代码:

 Email email = new SimpleEmail();
    email.setHostName("smtp.googlemail.com");
    email.setSmtpPort(465);
    email.setAuthenticator(new DefaultAuthenticator("ruth.sistem@gmail.com", "XXXXXX"));
    email.setSSLOnConnect(true);
    email.setFrom("ruth.sistem@gmail.com");
    email.setSubject("TestMail");
    email.setMsg("This is a test mail ... :-)");
    email.addTo("ruth.sistem@gmail.com");
    email.send();

我无法连接到gmail,出现连接错误,但是所有连接信息都是正确的,不知道什么阻止了我的代码发送电子邮件的连接,我不能发送简单的电子邮件而且我做的没有丝毫知道它是什么。

 org.apache.commons.mail.EmailException: Sending the email to the following server failed :                                                          smtp.googlemail.com:465
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)
at org.apache.commons.mail.Email.send(Email.java:1437)
at com.observatorioLegislativo.util.EmailTeste.enviaEmailSimples(EmailTeste.java:27)
at com.observatorioLegislativo.util.EmailTeste.<init>(EmailTeste.java:13)
at com.observatorioLegislativo.bean.Teste.main(Teste.java:41)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.googlemail.com,  port: 465;
 nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)
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 org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
... 4 more


Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:317)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:207)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938)
... 11 more

欣赏帮助

5 个答案:

答案 0 :(得分:7)

我认为代码段取自Apache Commons Email API User Guide

GMail会出于安全原因默认阻止 Access for less secure apps ,因此这可能会导致您的问题(就像我的那样)。

登录您的GMail帐户并转到以下网址:

https://www.google.com/settings/security/lesssecureapps

enter image description here

设为 Enable

Email email = new SimpleEmail();

try {
    email.setHostName("smtp.googlemail.com");
    email.setSmtpPort(465);
    email.setAuthenticator(
        new DefaultAuthenticator("hogwarts-mailer@gmail.com", "password"));
    email.setSSLOnConnect(true); // disable in case of EmailException
    email.setFrom("hogwarts-school@gmail.com", "Hogwarts School");
    email.setSubject("Hogwarts Acceptance Letter");
    email.setMsg("We are pleased to inform you that you have a place at "
        + "Hogwarts School of Witchcraft and Wizardry.");
    email.addTo("harry-potter@hedwig.com");
    email.send();

} catch(EmailException ee) {
    ee.printStackTrace();
}
  

<强>声明:

     

通过这样做,您已经禁用了GMail的一项安全功能。   您需要自担风险(或使用虚拟电子邮件)。

相关阅读: Sending E-Mail Using GMail SMTP via Apache Commons Emails

答案 1 :(得分:0)

尝试smtp.gmail.com和端口587.这些是我使用的设置。 (使用STARTTLS,但用于身份验证的“普通”密码......我相信您的SSLOnConnect就像它一样好)

答案 2 :(得分:0)

JavaMail FAQ有tips for debugging connection problems

最有可能是防火墙或防病毒程序阻止您连接。

答案 3 :(得分:0)

不需要尝试,仅捕获更改gmail中的设置以允许安全性较低的应用程序即可。

只需转到:

1。我的帐户>>登录和安全>>滚动到页面底部,您将看到以下部分:“ 允许不太安全的应用程序:关闭

2. 只需将其打开即可。

图:Secure access image

答案 4 :(得分:0)

当我尝试使用ktorSimpleEmail服务器应用发送电子邮件时,这也发生在我身上。我的案例的不同之处在于,我在自定义g-suit域中启用了两步验证,并且无法使用“安全性较低的应用”设置:

  

此设置不适用于启用了两步验证的帐户。此类帐户需要使用特定于应用程序的密码才能降低应用程序访问的安全性

我生成了应用专用密码,并使用它代替了我的主要Gmail密码,并且可以使用。

        val email = SimpleEmail()
        email.hostName = "smtp.gmail.com"
        email.setSmtpPort(587)
        email.setAuthenticator(DefaultAuthenticator("info@customdomain.app", "APPLICATION_SPECIFIC_PASSWORD"))
        email.isSSLOnConnect = true
        email.setFrom("info@customdomain.app")
        email.subject = "TestMail"
        email.setMsg("This is a test mail... :-)")
        email.addTo("foo@bar.com")
        email.send()

Create app password info