0
I am trying to send email using google smtp with the Commonsmail lib, in a web application, but this always generating a fault.
I made a separate app and tested several settings, in which I saw several other topics. Using port 465 and 587 with and without SSL and TLS, with Sessionemail from Glassfish 5, with the win10 firewall disabled and enabled, in versions other than Commons mail, but nothing is working. Note: I marked to Allow less secure applications to access the gmail I created for testing.
Someone could help me with this problem, I would be very grateful, I have no more ideas.
Maven repository for Commons Mail
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.5</version>
</dependency>
Code;
public void enviarEmail() throws EmailException { Email email = new SimpleEmail(); email.setDebug(true); email.setHostName("smtp.googlemail.com"); email.setStartTLSRequired(true); email.setSmtpPort(465); email.setAuthenticator(new DefaultAuthenticator("[email protected]","teste123")); email.setSSLOnConnect(true); email.setSslSmtpPort("587"); email.setFrom("[email protected]"); email.setStartTLSRequired(true); email.setSSLCheckServerIdentity(true); email.addTo("[email protected]"); email.send(); }
Error
Warning: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.googlemail.com:587 javax.el.ELException: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.googlemail.com:587 at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:187) at com.sun.el.parser.AstValue.invoke(AstValue.java:289) at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304) at org.jboss.weld.module.web.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) at org.jboss.weld.module.web.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:150) at javax.faces.event.ActionEvent.processListener(ActionEvent.java:96) at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:839) at javax.faces.component.UICommand.broadcast(UICommand.java:315) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:870) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1418) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:100) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:201) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:670) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1580) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:258) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:652) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:591) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:371) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:238) at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:463) at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:168) at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206) at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:242) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573) at java.lang.Thread.run(Thread.java:748) Caused by: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.googlemail.com:587 at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410) at org.apache.commons.mail.Email.send(Email.java:1437) at br.com.mail.mail.teste.EmailController.enviarEmail(EmailController.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.el.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:181) ... 42 more Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.googlemail.com, port: 587; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection? at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2196)
My friend, sorry for the delay.. I tried using this host too, but the error remains the same.
– Lucas
edited the answer, try to see if any works.
– Allan Braga
The only thing I didn’t agree with was the Jars, so I put the missing dependencies into Maven and tested all these ways to send again in more versions of javamail and Commons. Google’s email is configured to send, correctly in case allowing less secure apps.
– Lucas
and did not work? what error gave?
– Allan Braga
Unfortunately it did not work, continued with the same mistake. 
Esse teste estava usando seu exemplo "STARTTLS (Port 587) -> email.setStartTLSEnabled(true);"

Warning: #{emailController.enviarEmail}: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:587
javax.faces.FacesException: #{emailController.enviarEmail}: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:587
– Lucas
I doubt if it can be something in dependencies/libs, OS, or glassfish, because I did the same months ago and it worked, but now I switched to manager Maven, pro glassfish 5 and I am working on SO win10, however I did not find anything blocking.
– Lucas
Try replicating this same code in another environment to test.
– Allan Braga
It worked sending, but only in the java test class I did, when I Glassfish up to test the app, the same code in the controller shows the error Sending the email to following server failed : smtp.gmail.com:465. I tested with mail.Session that I set up in Glassfish, but it also doesn’t work.
– Lucas
Google’s smtp port is not the 465, but the 587, try and see if it works
– Allan Braga
and confirm that your jar for example smtp.jar is in your classpath.
– Allan Braga
Yes, I added the missing dependencies, the dsn jar, smtp, gimap, Activation, pop3, mailapi, Commons-email, javax.mail are all present, even though they are not all used kk Strange that I put setSmtpPort(587), but it takes port 465. One way I found to get to port 587 was to put setSslSmtpPort("587"), but I was also unsuccessful, but it changed the error ; Caused by: javax.mail.Messagingexception: Could not connect to SMTP host: smtp.gmail.com, port: 587; nested Exception is: javax.net.ssl.Sslexception: Unrecognized SSL message, plaintext Connection?
– Lucas
as to this error when changing Voce you have to enable secure ssl mail.smtp.ssl.enable to true
– Allan Braga
Now it worked, the last problem was the version of Glassfish that was using, in the case of 5.0. After finding this thread https://github.com/javaee/glassfish/issues/22436 I passed the test with Glassfish 5.0.1 and Glassfish 4.1.1, so it worked perfectly without having to change the code. Thank you so much for your personal help. TMJ
– Lucas