Play 2.5 with mailer plugin

2019-09-14 21:30发布

问题:

I am using below config to send email through play app:

smtp.mock=false
smtp {
        host="smtp.mail.yahoo.com"
        port="465"
        ssl=true
        user="pari.test@yahoo.com"
        password="XXXXXXXXXXX"
        from="pari.test@yahoo.com"
}

Code works all good. However, I am getting warning:

smtp is deprecated, use play.mailer instead.

When I use play.mailer configs code fails. config

play.mailer {
    host="smtp.mail.yahoo.com"
    port=465
    ssl=yes
    tls=no
    user="pari.test@yahoo.com"
    password="XXXXXX"
    debug=no
    timeout=60
    connectiontimeout=60
    mock=false
}

Error :

     [error] application - error sending email
org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.mail.yahoo.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 utils.Mailer.sendEmail(Mailer.scala:30)
    at utils.Mailer$$anonfun$sendEmailAsync$1.apply$mcV$sp(Mailer.scala:22)
    at utils.Mailer$$anonfun$sendEmailAsync$1.apply(Mailer.scala:22)
    at utils.Mailer$$anonfun$sendEmailAsync$1.apply(Mailer.scala:22)
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.yahoo.com, port: 465
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    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)
    at play.api.libs.mailer.SMTPMailer$$anon$2.send(MailerPlugin.scala:100)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:668)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:211)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927

Source code is available at https://github.com/pariksheet/dribble.

Thanks Pari

回答1:

All you need: modify 60 (s) -> 60000 (ms)

play.mailer { host (mandatory) port (defaults to 25) ssl (defaults to no) tls (defaults to no) user (optional) password (optional) debug (defaults to no, to take effect you also need to set the log level to "DEBUG" for the application logger) timeout (defaults to 60s in milliseconds) connectiontimeout (defaults to 60s in milliseconds) mock (defaults to no, will only log all the email properties instead of sending an email) }