使用Play 2.5播放Mailer插件

时间:2016-08-22 10:42:04

标签: scala playframework

我正在努力使用播放邮件插件。我发现许多引用配置play-mailer插件来使用gmail发送电子邮件,但都没有。

以下是详细信息 -

  1. 播放2.5
  2. Scala 2.11
  3. 邮件插件 - "com.typesafe.play" %% "play-mailer" % "5.0.0"
  4. 这是我的代码:

    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
    }
    

    控制器:

    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?")
        }
    }
    

    错误:

    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)
    

    有什么想法吗?

    P.S。我也为gmail帐户启用了不太安全的访问权限。

2 个答案:

答案 0 :(得分:2)

您只需要在application.conf中提供播放邮件程序配置,如下所示

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"

答案 1 :(得分:0)

我已经用smtp.mock替换了play.mailer块。在此更改后,我收到应用程序发送的电子邮件。

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

然而,我正在收到警告 - smtp已弃用,请改用play.mailer

任何人都有任何想法,如何配置邮件配置。

由于 彩