CommonsMail JavaMail - Failed to send email

0

I'm trying to send email using google's smtp with the CommonsMail lib in a web application, but it's always causing a flaw.

I made a separate app and tested several settings, which I've been seeing on a number of other topics. Using port 465 and 587 with and without SSL and TLS, with Glassfish 5's SessionEmail, with the win10 firewall disabled and enabled, in different versions of commons mail, but nothing is working.  Note: I checked Allow less secure apps to access the gmail I created for testing.

Someone could help me with this problem, I would be grateful, I have no more ideas left.

Commons Mail Maven Repository

 <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)
    
asked by anonymous 22.05.2018 / 23:42

1 answer

0

You are not setting the host correctly for gmail. Example:

email.setHostName("smtp.gmail.com");

Possible solutions

1- Verify that you are importing all jars correctly:

dsn.jar, imap.jar, mailapi.jar, pop3.jar, smtp.jar.

2 - Make sure your email is set up to send emails:

My Account > Login and Security > below, look for "Allow less secure apps:" if it is disabled, just enable it.

Here are examples of how to send using port 465 and 587:

SSL / TLS (Port 465) - > email.setSSLOnConnect (true);

Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setSSLOnConnect(true);
email.setFrom("[email protected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[email protected]");
email.send();

STARTTLS (Port 587) - > email.setStartTLSEnabled (true);

Email email = new SimpleEmail();
email.setHostName("smtp.gmail.com");
email.setSmtpPort(587);
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.setStartTLSEnabled(true);
email.setFrom("[email protected]");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("[email protected]");
email.send();
    
23.05.2018 / 17:33