Not able to send emails from play mailer plugin

2019-08-14 14:45发布

问题:

I'm trying to send emails from my play application. currently I am running application on localhost. for email part I was following this https://github.com/playframework/play-mailer and so far implemented this: I ahve added this to build.sbt dependencies:

 "com.typesafe.play" %% "play-mailer" % "5.0.0-M1"

This is HomeController.Java:

 //MAiler Inject
private final MailerClient mailer;
@Inject
public HomeController(MailerClient mailer)
{
    this.mailer = mailer;
}
public void sendEmail() {
    String cid = "1234";
    Email email = new Email();
    email.setSubject("Simple email");
    email.setFrom("from@gmail.com");
    email.addTo("to@gmail.com");
    // adds attachment
    // .addAttachment("attachment.pdf", new File("/some/path/attachment.pdf"))
    // adds inline attachment from byte array
    //   .addAttachment("data.txt", "data".getBytes(), "text/plain", "Simple data", EmailAttachment.INLINE)
    // adds cid attachment
    // .addAttachment("image.jpg", new File("/some/path/image.jpg"), cid)
    // sends text, HTML or both...
    email .setBodyText("A text message");
    //    .setBodyHtml("<html><body><p>An <b>html</b> message with cid <img src=\"cid:" + cid + "\"></p></body></html>");
    mailer.send(email);
}

This is application.config

play.mailer {
host="smtp.gmail.com"
port=465
ssl=yes
tls=no
user="myid@gmail.com"
password="mypass"
debug=no
timeout=60
connectiontimeout=60
mock=no
}

Exception:

[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.gmail.com", port 465, isSSL false
[debug] application - 

[error] application - 

! @6pgje8o34 - Internal server error, for (POST) [/addUser] ->

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:269)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:195)
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:99)
at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:98)
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)

But, I'm not getting emails.

I'm using play 2.5.

回答1:

Solved the Problem.. Configuration and codes were all right. Gmail's security settings were blocking the mail, you can turn on less secure settings (but with caution) from here... https://www.google.com/settings/security/lesssecureapps. and it all will work like a charm. :)