Play Mailer plugin with Play 2.5

2019-09-09 07:02发布

问题:

I am struggling with play mailer plugin. I found many references to configure play-mailer plugin to send email using gmail, but none worked.

Here are the details -

  1. Play 2.5
  2. Scala 2.11
  3. mailer plugin - "com.typesafe.play" %% "play-mailer" % "5.0.0"

And here is my code:

application.conf:

play.mailer {
    host="smtp.gmail.com"
    port=587
    ssl=no
    tls=yes
    user="pariXXXXXX@gmail.com"
    password="XXXXXX"
    debug=no
    timeout=60
    connectiontimeout=60
    mock=false
}

Controller:

class HomeController @Inject() (
    val messagesApi: MessagesApi,
    val mailer:MailerClient)
extends Controller with I18nSupport {
    def index = Action {
        val bodyHtml = Some(views.html.mails.welcome("Pari").toString)
        val email = Email(subject = "subject", from = "pari.XXXXX@gmail.com", to = List("pari.XXXXX@gmail.com"), bodyHtml = bodyHtml, bodyText = Some("Hello"), replyTo = None)
        mailer.send(email)
        Ok("Did you receive my email?")
    }
}

Error:

Execution exception[[EmailException: Sending the email to the following server failed : smtp.gmail.com:587]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
    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:98)
    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: org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.gmail.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 controllers.HomeController$$anonfun$index$1.apply(HomeController.scala:82)
    at controllers.HomeController$$anonfun$index$1.apply(HomeController.scala:79)
    at play.api.mvc.ActionBuilder$$anonfun$apply$14.apply(Action.scala:403)
    at play.api.mvc.ActionBuilder$$anonfun$apply$14.apply(Action.scala:403)
Caused by: javax.mail.MessagingException: Exception reading response
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2202)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1939)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    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:1411)
    at org.apache.commons.mail.Email.send(Email.java:1448)
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
    at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2182)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1939)

Any thoughts?

P.S. I have enabled less secure access for gmail account as well.

回答1:

You simply need to provide play mailer configuration in your application.conf as below

play.mailer.host= "smtp.gmail.com"
play.mailer.port= "46"
play.mailer.ssl= true
play.mailer.user="pari.XXXX@gmail.com"
play.mailer.password="XXXXXXXXXX"


回答2:

I have replaced play.mailer block with smtp.mock. After this change I am getting email sent by application.

smtp.mock=false
smtp {
        host="smtp.gmail.com" #example: 
        port="465" #example: 465
        ssl=true
        user="pari.XXXX@gmail.com"
        password="XXXXXXXXXX"
        from="XXXXXXX"
}

However, I am getting warning - smtp is deprecated, use play.mailer instead

Anybody has any idea, how to configure mailer configs.

Thanks Pari