0

I have the following code:

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

I can not connect to gmail, an error burst with connection appears, however all connection information are correct, do not know what is blocking the connection from my code for sending the email, I can not send a simple email and I do not slightest idea what it is.

 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

appreciate the help

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Ruth
  • 21
  • 1
  • 3
  • 6

6 Answers6

8

I believe that snippet is taken from the Apache Commons Email API User Guide.

GMail blocks Access for less secure apps by default for security reasons so this might be causing your problem (as it did mine).

Log in to your GMail account and go to this URL:

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

enter image description here

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

DISCLAIMER:

By doing so, you have disabled one of GMail's security features. Proceed at your own risk (or use a dummy email).

Related reading: Sending E-Mail Using GMail SMTP via Apache Commons Emails

k_rollo
  • 5,304
  • 16
  • 63
  • 95
  • 2
    I had the same issue and this worked for me. My question is: Is there any way that we do not need to lower the security in gmail? – mohi Mar 08 '15 at 10:52
  • I have not tested for other methods since I used a dummy email for this. – k_rollo Mar 12 '15 at 08:53
  • 1
    Remember check out the firewall is not blocking the port – Jorge Nunez Newton Sep 30 '15 at 21:13
  • This worked for me. Before lowering the security, gmail sent me a mail that it blocked sending mail via less secure app. Once enabled, I received a mail about it. Post that I am able to send mails – usct01 May 11 '17 at 16:02
1

Not required to put it in try and catch only changing the settings in gmail to allow lesser secure apps will do.

Just go to:

1.My account>>Sign in & Security>>Scroll to the bottom of the page and u will see the section:"Allow less secure apps: OFF"

2.Just turn it on and it will be done.

Img:Secure access image

1

This happened to me as well when I tried to send email from ktor server app using SimpleEmail. The difference in my case was that I had my email at custom g-suit domain with 2-step verification turned on and I couldn't use "less secure apps" settings:

This setting is not available for accounts with 2-Step Verification enabled. Such accounts require an application-specific password for less secure apps access

I generated app spefific password and used it instead of my main gmail password and it worked.

        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

Ondřej Z
  • 5,094
  • 3
  • 24
  • 30
1

Since Google has disabled 3rd party app access you may need to create an App password and use that in your application it may work.

you can create an app password from here.

    val email = SimpleEmail()
    email.hostName = "smtp.googlemail.com"
    email.setSmtpPort(587)
    email.setAuthenticator(DefaultAuthenticator("YOUR_EMAIL", "APP_PASSWORD"))
    email.isSSLOnConnect = true
    email.setFrom("YOUR_EMAIL")
    email.subject = "Test Email"
    email.setMsg("Mail testing")
    email.addTo("recipient email.")
    val mime = email.send()
0

Try smtp.gmail.com, and Port 587. These are the settings I use. (With STARTTLS, but 'normal' password for authentication....I believe your SSLOnConnect is fine the way it is)

Grantly
  • 2,546
  • 2
  • 21
  • 31
0

The JavaMail FAQ has tips for debugging connection problems.

Most likely there's a firewall or antivirus program preventing you from connecting.

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40