Click here to open the repository in the Maven Repository for Apache Commons Email.
I have the following code that receives a recipient, subject and message from a form:
@RequestMapping(value = { "/email_commons/send" }, method = RequestMethod.POST, params = {"subject", "message", "to"})
public @ResponseBody Boolean enviaEmail(
@RequestParam(value = "subject") String subject,
@RequestParam(value = "message") String message,
@RequestParam(value = "to") String to,
HttpServletRequest request) throws Exception {
Email email = new SimpleEmail();
email.setDebug(true);
email.setHostName("smtp.gmail.com");
email.setSmtpPort(465);
email.setAuthentication("[email protected]", "senha");
email.setSSL(true);
email.setTLS(true);
email.setFrom("[email protected]");
email.setSubject(subject);
email.setMsg(message);
email.addTo(to);
try {
email.send();
return true;
} catch (Exception e){
e.printStackTrace();
return false;
}
}
But when you try to send from the line email.send()
returns false and this appears in console :
10:53:05,854 INFO [stdout] (default task-18) DEBUG: JavaMail version 1.5.3
10:53:05,855 INFO [stdout] (default task-18) DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
10:53:05,855 INFO [stdout] (default task-18) DEBUG: Tables of loaded providers
10:53:05,856 INFO [stdout] (default task-18) DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
10:53:05,856 INFO [stdout] (default task-18) DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
10:53:05,856 INFO [stdout] (default task-18) DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
10:53:05,857 INFO [stdout] (default task-18) DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
10:53:05,962 INFO [stdout] (default task-18) DEBUG SMTP: need username and password for authentication
10:53:05,963 INFO [stdout] (default task-18) DEBUG SMTP: useEhlo true, useAuth true
10:53:05,963 INFO [stdout] (default task-18) DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
10:54:06,063 INFO [stdout] (default task-18) DEBUG SMTP: exception reading response, THROW:
10:54:06,064 INFO [stdout] (default task-18) java.net.SocketTimeoutException: Read timed out
10:54:06,064 INFO [stdout] (default task-18) at java.net.SocketInputStream.socketRead0(Native Method)
10:54:06,065 INFO [stdout] (default task-18) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
10:54:06,065 INFO [stdout] (default task-18) at java.net.SocketInputStream.read(SocketInputStream.java:170)
10:54:06,065 INFO [stdout] (default task-18) at java.net.SocketInputStream.read(SocketInputStream.java:141)
10:54:06,065 INFO [stdout] (default task-18) at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)
10:54:06,065 INFO [stdout] (default task-18) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
10:54:06,067 INFO [stdout] (default task-18) at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
10:54:06,067 INFO [stdout] (default task-18) at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
10:54:06,067 INFO [stdout] (default task-18) at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2285)
10:54:06,067 INFO [stdout] (default task-18) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2032)
10:54:06,068 INFO [stdout] (default task-18) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
10:54:06,068 INFO [stdout] (default task-18) at javax.mail.Service.connect(Service.java:386)
10:54:06,068 INFO [stdout] (default task-18) at javax.mail.Service.connect(Service.java:245)
10:54:06,068 INFO [stdout] (default task-18) at javax.mail.Service.connect(Service.java:194)
10:54:06,068 INFO [stdout] (default task-18) at javax.mail.Transport.send0(Transport.java:253)
10:54:06,074 INFO [stdout] (default task-18) at javax.mail.Transport.send(Transport.java:124)
10:54:06,076 INFO [stdout] (default task-18) at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1411)
10:54:06,076 INFO [stdout] (default task-18) at org.apache.commons.mail.Email.send(Email.java:1448)
10:54:06,076 INFO [stdout] (default task-18) at controller.EmailController.baixarContaPagar(EmailController.java:42)
10:54:06,077 INFO [stdout] (default task-18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
10:54:06,077 INFO [stdout] (default task-18) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
10:54:06,077 INFO [stdout] (default task-18) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
10:54:06,078 INFO [stdout] (default task-18) at java.lang.reflect.Method.invoke(Method.java:497)
10:54:06,078 INFO [stdout] (default task-18) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
10:54:06,078 INFO [stdout] (default task-18) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
10:54:06,078 INFO [stdout] (default task-18) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
10:54:06,079 INFO [stdout] (default task-18) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
10:54:06,079 INFO [stdout] (default task-18) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
10:54:06,079 INFO [stdout] (default task-18) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
10:54:06,079 INFO [stdout] (default task-18) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
10:54:06,079 INFO [stdout] (default task-18) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
10:54:06,080 INFO [stdout] (default task-18) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
10:54:06,080 INFO [stdout] (default task-18) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
10:54:06,080 INFO [stdout] (default task-18) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
10:54:06,081 INFO [stdout] (default task-18) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
10:54:06,081 INFO [stdout] (default task-18) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
10:54:06,081 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
10:54:06,081 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
10:54:06,081 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
10:54:06,082 INFO [stdout] (default task-18) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
10:54:06,082 INFO [stdout] (default task-18) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:54:06,082 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
10:54:06,082 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
10:54:06,082 INFO [stdout] (default task-18) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:54:06,083 INFO [stdout] (default task-18) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
10:54:06,083 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
10:54:06,083 INFO [stdout] (default task-18) at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
10:54:06,083 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
10:54:06,083 INFO [stdout] (default task-18) at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
10:54:06,084 INFO [stdout] (default task-18) at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
10:54:06,084 INFO [stdout] (default task-18) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:54:06,084 INFO [stdout] (default task-18) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
10:54:06,084 INFO [stdout] (default task-18) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:54:06,084 INFO [stdout] (default task-18) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:54:06,085 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
10:54:06,085 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
10:54:06,085 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
10:54:06,085 INFO [stdout] (default task-18) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
10:54:06,085 INFO [stdout] (default task-18) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
10:54:06,085 INFO [stdout] (default task-18) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
10:54:06,086 INFO [stdout] (default task-18) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
10:54:06,086 INFO [stdout] (default task-18) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
10:54:06,086 INFO [stdout] (default task-18) at java.lang.Thread.run(Thread.java:745)
10:54:06,089 ERROR [stderr] (default task-18) org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
10:54:06,089 ERROR [stderr] (default task-18) at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1421)
10:54:06,089 ERROR [stderr] (default task-18) at org.apache.commons.mail.Email.send(Email.java:1448)
10:54:06,090 ERROR [stderr] (default task-18) at controller.EmailController.baixarContaPagar(EmailController.java:42)
10:54:06,090 ERROR [stderr] (default task-18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
10:54:06,090 ERROR [stderr] (default task-18) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
10:54:06,090 ERROR [stderr] (default task-18) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
10:54:06,090 ERROR [stderr] (default task-18) at java.lang.reflect.Method.invoke(Method.java:497)
10:54:06,090 ERROR [stderr] (default task-18) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
10:54:06,090 ERROR [stderr] (default task-18) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
10:54:06,090 ERROR [stderr] (default task-18) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
10:54:06,090 ERROR [stderr] (default task-18) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
10:54:06,090 ERROR [stderr] (default task-18) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
10:54:06,091 ERROR [stderr] (default task-18) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
10:54:06,091 ERROR [stderr] (default task-18) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
10:54:06,091 ERROR [stderr] (default task-18) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
10:54:06,091 ERROR [stderr] (default task-18) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
10:54:06,091 ERROR [stderr] (default task-18) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
10:54:06,091 ERROR [stderr] (default task-18) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
10:54:06,091 ERROR [stderr] (default task-18) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
10:54:06,091 ERROR [stderr] (default task-18) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
10:54:06,091 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
10:54:06,091 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
10:54:06,092 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
10:54:06,092 ERROR [stderr] (default task-18) at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
10:54:06,092 ERROR [stderr] (default task-18) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:54:06,092 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
10:54:06,092 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
10:54:06,092 ERROR [stderr] (default task-18) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:54:06,093 ERROR [stderr] (default task-18) at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
10:54:06,093 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
10:54:06,093 ERROR [stderr] (default task-18) at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
10:54:06,093 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
10:54:06,093 ERROR [stderr] (default task-18) at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
10:54:06,093 ERROR [stderr] (default task-18) at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
10:54:06,093 ERROR [stderr] (default task-18) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:54:06,094 ERROR [stderr] (default task-18) at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
10:54:06,094 ERROR [stderr] (default task-18) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:54:06,094 ERROR [stderr] (default task-18) at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
10:54:06,094 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
10:54:06,094 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
10:54:06,094 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
10:54:06,094 ERROR [stderr] (default task-18) at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
10:54:06,094 ERROR [stderr] (default task-18) at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
10:54:06,095 ERROR [stderr] (default task-18) at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
10:54:06,095 ERROR [stderr] (default task-18) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
10:54:06,095 ERROR [stderr] (default task-18) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
10:54:06,095 ERROR [stderr] (default task-18) at java.lang.Thread.run(Thread.java:745)
10:54:06,095 ERROR [stderr] (default task-18) Caused by: javax.mail.MessagingException: Exception reading response;
10:54:06,095 ERROR [stderr] (default task-18) nested exception is:
10:54:06,095 ERROR [stderr] (default task-18) java.net.SocketTimeoutException: Read timed out
10:54:06,095 ERROR [stderr] (default task-18) at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2305)
10:54:06,095 ERROR [stderr] (default task-18) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2032)
10:54:06,095 ERROR [stderr] (default task-18) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
10:54:06,095 ERROR [stderr] (default task-18) at javax.mail.Service.connect(Service.java:386)
10:54:06,096 ERROR [stderr] (default task-18) at javax.mail.Service.connect(Service.java:245)
10:54:06,096 ERROR [stderr] (default task-18) at javax.mail.Service.connect(Service.java:194)
10:54:06,096 ERROR [stderr] (default task-18) at javax.mail.Transport.send0(Transport.java:253)
10:54:06,096 ERROR [stderr] (default task-18) at javax.mail.Transport.send(Transport.java:124)
10:54:06,096 ERROR [stderr] (default task-18) at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1411)
10:54:06,096 ERROR [stderr] (default task-18) ... 46 more
10:54:06,096 ERROR [stderr] (default task-18) Caused by: java.net.SocketTimeoutException: Read timed out
10:54:06,096 ERROR [stderr] (default task-18) at java.net.SocketInputStream.socketRead0(Native Method)
10:54:06,096 ERROR [stderr] (default task-18) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
10:54:06,096 ERROR [stderr] (default task-18) at java.net.SocketInputStream.read(SocketInputStream.java:170)
10:54:06,096 ERROR [stderr] (default task-18) at java.net.SocketInputStream.read(SocketInputStream.java:141)
10:54:06,097 ERROR [stderr] (default task-18) at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)
10:54:06,097 ERROR [stderr] (default task-18) at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
10:54:06,097 ERROR [stderr] (default task-18) at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
10:54:06,097 ERROR [stderr] (default task-18) at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
10:54:06,097 ERROR [stderr] (default task-18) at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2285)
10:54:06,097 ERROR [stderr] (default task-18) ... 54 more
What can it be? I have already changed the SMTP ports to 465, 487 and 25, HostName, I moved with the SSL settings, but nothing solved.