Reputation: 21
I have the following code:
Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("[email protected]", "XXXXXX"));
email.setSSLOnConnect(true);
email.setFrom("[email protected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[email protected]");
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
Upvotes: 0
Views: 12070
Reputation: 121
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()
Upvotes: 1
Reputation: 5174
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("[email protected]", "APPLICATION_SPECIFIC_PASSWORD"))
email.isSSLOnConnect = true
email.setFrom("[email protected]")
email.subject = "TestMail"
email.setMsg("This is a test mail... :-)")
email.addTo("[email protected]")
email.send()
Upvotes: 1
Reputation: 11
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.
Upvotes: 1
Reputation: 5472
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
Set to Enable
.
Email email = new SimpleEmail();
try {
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(
new DefaultAuthenticator("[email protected]", "password"));
email.setSSLOnConnect(true); // disable in case of EmailException
email.setFrom("[email protected]", "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("[email protected]");
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
Upvotes: 8
Reputation: 29971
The JavaMail FAQ has tips for debugging connection problems.
Most likely there's a firewall or antivirus program preventing you from connecting.
Upvotes: 0
Reputation: 2556
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)
Upvotes: 0