我试图从我的播放应用程序发送电子邮件。目前我在localhost上运行应用程序。对于电子邮件部分我正在关注此https://github.com/playframework/play-mailer,到目前为止实现了这个: 我把它添加到build.sbt依赖项:
"com.typesafe.play" %% "play-mailer" % "5.0.0-M1"
这是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);
}
这是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
}
例外:
[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)
但是,我没有收到电子邮件。
我正在使用游戏2.5。
答案 0 :(得分:3)
解决了问题..配置和代码都没问题。 Gmail的安全设置阻止了邮件,您可以从此处打开安全性较低的设置(但请谨慎)...... https://www.google.com/settings/security/lesssecureapps。这一切都会像魅力一样。 :)