Skip to content

Unable to send Email with outlook or gmail config in Play 2.5 and mailer 5.0 plugin #115

@fahimmahmoodmir

Description

@fahimmahmoodmir

I also have been playing with mailer plugin but still i am having problem since 2 days i am stuck on just this simple task. Well reading other topics seems like other people are also have problems. So like them i also followed the same config as told in the installation in play-mailer page still not able to send any email. Here is my app configuration:

play.mailer {
  host=smtp.office365.com
  port=587
  ssl=false
  tls=true
  user="l************y@outlook.com"
  password="**************"
  debug=yes
}

MailService Class wrapper (omitted importing libs just to keep the code minimal):

public class MailerService {
    MailerClient mailerClient;

    @Inject
    public MailerService(MailerClient mailerClient) {
        this.mailerClient = mailerClient;
    }

    public void sendEmail() {
        String cid = "1234";
        Email email = new Email();
        email.setSubject("Verify Account Please");
        email.setFrom("*************@outlook.com");
        email.addTo("************@gmail.com");
        email.setBodyText("ABC");

        mailerClient.send(email);
    }
}

My controller action in the play framework:

public class ApiController extends Controller {
    MailerService mailerClient;

    @Inject
    public ApiController(MailerService mailer) {
        this.mailerClient = mailer;
    }

    public Result createUser() {
      //some code for app logic and then email
      mailerClient.sendEmail();
   }
}

and i getting these exceptions:

DEBUG: JavaMail version 1.5.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
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.SMTPTrans
port=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]}
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,Or
acle], 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]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
[debug] application - DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
[debug] application -

[debug] application - DEBUG SMTP: need username and password for authentication
[debug] application -

[debug] application - DEBUG SMTP: useEhlo true, useAuth true
[debug] application -

[debug] application - DEBUG SMTP: trying to connect to host "smtp.office365.com", port 587, isSSL false
[debug] application -

[debug] application - 220 HE1PR0401CA0077.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sat, 25 Feb 2017 12:27:17 +0000
[debug] application -

[debug] application - DEBUG SMTP: connected to host "smtp.office365.com", port: 587

[debug] application -

[debug] application - EHLO Fahim-PC
[debug] application -

[debug] application - 250-HE1PR0401CA0077.outlook.office365.com Hello [131.246.232.76]
[debug] application -

[debug] application - 250-SIZE 157286400
[debug] application -

[debug] application - 250-PIPELINING
[debug] application -

[debug] application - 250-DSN
[debug] application -

[debug] application - 250-ENHANCEDSTATUSCODES
[debug] application -

[debug] application - 250-STARTTLS
[debug] application -

[debug] application - 250-8BITMIME
[debug] application -

[debug] application - 250 SMTPUTF8
[debug] application -

[debug] application - DEBUG SMTP: Found extension "SIZE", arg "157286400"
[debug] application -

[debug] application - DEBUG SMTP: Found extension "PIPELINING", arg ""
[debug] application -

[debug] application - DEBUG SMTP: Found extension "DSN", arg ""
[debug] application -

[debug] application - DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
[debug] application -

[debug] application - DEBUG SMTP: Found extension "STARTTLS", arg ""
[debug] application -

[debug] application - DEBUG SMTP: Found extension "8BITMIME", arg ""
[debug] application -

[debug] application - DEBUG SMTP: Found extension "SMTPUTF8", arg ""
[debug] application -

[debug] application - STARTTLS
[debug] application -

[debug] application - 220 2.0.0 SMTP server ready
[debug] application -

[error] application -

! @736o4gfc9 - Internal server error, for (POST) [/api/user/] ->

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: org.apache.commons.mail.EmailException: Sending the email to the following server failed :
 smtp.office365.com:587]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
        at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
        at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
        at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.office365.com:587
        at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
        at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
        at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:593)
        at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
        at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)
        at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977)
        at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:21)
        at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:18)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
        at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)
Caused by: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.office365.com:587
        at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1421)
        at org.apache.commons.mail.Email.send(Email.java:1448)
        at play.api.libs.mailer.SMTPMailer$$anon$2.send(MailerPlugin.scala:100)
        at play.api.libs.mailer.CommonsMailer.send(MailerPlugin.scala:130)
        at play.api.libs.mailer.SMTPMailer.send(MailerPlugin.scala:110)
        at play.api.libs.mailer.SMTPDynamicMailer.send(MailerPlugin.scala:117)
        at play.api.libs.mailer.MailerClient$class.send(MailerPlugin.scala:56)
        at play.api.libs.mailer.SMTPDynamicMailer.send(MailerPlugin.scala:114)
        at services.MailerService.sendEmail(MailerService.java:29)
        at controllers.ApiController.createUser(ApiController.java:104)
Caused by: javax.mail.MessagingException: Could not convert socket to TLS
        at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1999)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:709)
        at javax.mail.Service.connect(Service.java:386)
        at javax.mail.Service.connect(Service.java:245)
        at javax.mail.Service.connect(Service.java:194)
        at javax.mail.Transport.send0(Transport.java:253)
        at javax.mail.Transport.send(Transport.java:124)
        at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1411)
        at org.apache.commons.mail.Email.send(Email.java:1448)
        at play.api.libs.mailer.SMTPMailer$$anon$2.send(MailerPlugin.scala:100)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unab
le to find valid certification path to requested target
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
        at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)
        at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)
        at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
        at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
        at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path t
o requested target
        at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)
        at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
        at sun.security.validator.Validator.validate(Validator.java:260)
        at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
        at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
        at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)
        at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
        at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
        at sun.security.ssl.Handshaker.process_record(Handshaker.java:914)
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
        at sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)
        at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)
        at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)
        at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)
        at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
        at sun.security.validator.Validator.validate(Validator.java:260)
        at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
        at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
        at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
        at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)

When i go for gmail config then my app config looks like this:

play.mailer {
  host="smtp.gmail.com"
  port=465
  ssl=yes
  user="*****@gmail.com"
  password="******"
  debug=yes
  timeout=60000
  connectiontimeout=60000
}

And exceptions:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: org.apache.commons.mail.EmailException: Sending the email to the following server failed :
 smtp.gmail.com:465]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220)
        at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
        at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
        at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: java.util.concurrent.CompletionException: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
        at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
        at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
        at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:593)
        at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
        at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)
        at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977)
        at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:21)
        at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:18)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
        at scala.concurrent.BatchingExecutor$Batch$$anonfun$run$1.processBatch$1(BatchingExecutor.scala:63)
Caused by: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.com:465
        at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1421)
        at org.apache.commons.mail.Email.send(Email.java:1448)
        at play.api.libs.mailer.SMTPMailer$$anon$2.send(MailerPlugin.scala:100)
        at play.api.libs.mailer.CommonsMailer.send(MailerPlugin.scala:130)
        at play.api.libs.mailer.SMTPMailer.send(MailerPlugin.scala:110)
        at play.api.libs.mailer.SMTPDynamicMailer.send(MailerPlugin.scala:117)
        at play.api.libs.mailer.MailerClient$class.send(MailerPlugin.scala:56)
        at play.api.libs.mailer.SMTPDynamicMailer.send(MailerPlugin.scala:114)
        at services.MailerService.sendEmail(MailerService.java:29)
        at controllers.ApiController.createUser(ApiController.java:104)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2055)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
        at javax.mail.Service.connect(Service.java:386)
        at javax.mail.Service.connect(Service.java:245)
        at javax.mail.Service.connect(Service.java:194)
        at javax.mail.Transport.send0(Transport.java:253)
        at javax.mail.Transport.send(Transport.java:124)
        at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1411)
        at org.apache.commons.mail.Email.send(Email.java:1448)
        at play.api.libs.mailer.SMTPMailer$$anon$2.send(MailerPlugin.scala:100)
Caused by: java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:209)
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
        at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
        at sun.security.ssl.InputRecord.read(InputRecord.java:503)
        at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)
        at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)
        at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:543)
        at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:348)

Also i want to confirm something as i am using play without any SSL config so right now on my dev server i am accessing it locally on HTTP rather HTTPS. Does that matter that i have to be on HTTPS protocol to use this mailer plugin?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions