我需要使用Play Mailer從Play應用程序發送電子郵件(https://github.com/playframework/play-mailer)。Play Framework Mailer進程無法連接到GMail帳戶
在我的Gmail帳戶,我有兩個因素身份驗證開啓,這是公司的政策 - 我不能關掉。所以我使用的密碼是我通過GMail帳戶(https://support.google.com/mail/answer/185833?hl=en)生成的特定於應用程序的密碼。
我也無法打開「安全性較低的應用程序」(https://support.google.com/accounts/answer/6010255?hl=en)。
我已將此郵件的電子郵件和密碼值更改爲通用值。
玩耍梅勒,我有我的application.conf這些參數:
# Mailer
# ~~~~~
play.mailer {
host="smtp.gmail.com"
port=587
ssl=true
tls=true
user="[email protected]"
password="mypassword" # This is the application specific generated password
debug=false
mock=false
from="[email protected]"
}
我使用此代碼發送電子郵件:
/**
* Send mail with the new password.
*
* @param user
* user created
* @throws EmailException
* Exception when sending mail
*/
private void sendPasswordChanged(User user) throws EmailException {
String subject = Messages.get("mail.reset.confirm.subject");
String message = Messages.get("mail.reset.confirm.message");
Mail.Envelop envelop = new Mail.Envelop(subject, message, user.getEmail());
Mail mailer = new Mail(mailerClient);
mailer.sendMail(envelop);
}
從Mail.java調用此代碼類:
public void sendMail(Mail.Envelop envelop) {
EnvelopJob envelopJob = new EnvelopJob(envelop, mailerClient);
final FiniteDuration delay = Duration.create(DELAY, TimeUnit.SECONDS);
Akka.system().scheduler().scheduleOnce(delay, envelopJob, Akka.system().dispatcher());
}
static class EnvelopJob implements Runnable {
MailerClient mailerClient;
Mail.Envelop envelop;
@Inject
public EnvelopJob(Mail.Envelop envelop, MailerClient mailerClient) {
this.envelop = envelop;
this.mailerClient = mailerClient;
}
public void run() {
Email email = new Email();
final Configuration root = Configuration.root();
final String mailFrom = root.getString("play.mailer.from");
final String mailSign = root.getString("mail.sign");
email.setFrom(mailFrom);
email.setSubject(envelop.subject);
email.setBodyText(envelop.message + "\n\n " + mailSign);
email.setBodyHtml(envelop.message + "<br><br>--<br>" + mailSign);
for (String toEmail : envelop.toEmails) {
email.addTo(toEmail);
Logger.debug("Mail.sendMail: Mail will be sent to " + toEmail);
}
mailerClient.send(email);
Logger.debug("Mail sent - SMTP:" + root.getString("play.mailer.host")
+ ":" + root.getString("play.mailer.port")
+ " SSL:" + root.getString("play.mailer.ssl")
+ " user:" + root.getString("play.mailer.user")
+ " password:" + root.getString("play.mailer.password"));
}
}
當application.conf設置mock=true
,我可以看到正在與所有的正確的價值觀正確地創建了電子郵件:
[info] play.api.Play - Application started (Dev)
[info] application - mock implementation, send email
[info] application - subject: Account Access Attempted
[info] application - from: [email protected]
[info] application - bodyText: You (or someone else) entered this email address
when trying to change the password. However, this email address is not on our l
ist of registered users, and so the attempted password reset has failed.
null
[info] application - bodyHtml: You (or someone else) entered this email address
when trying to change the password. However, this email address is not on our l
ist of registered users, and so the attempted password reset has failed.<br><br>
--<br>null
[info] application - to: [email protected]
但是,當我設置mock=false
,我收到此錯誤:
[error] a.d.TaskInvocation - Sending the email to the following server failed :
smtp.gmail.com:587
org.apache.commons.mail.EmailException: Sending the email to the following serve
r failed : smtp.gmail.com:587
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410) ~[comm
ons-email-1.3.3.jar:1.3.3]
at org.apache.commons.mail.Email.send(Email.java:1437) ~[commons-email-1
.3.3.jar:1.3.3]
at play.api.libs.mailer.CommonsMailer$$anon$2.send(MailerPlugin.scala:10
2) ~[play-mailer_2.10-3.0.1.jar:3.0.1]
at play.api.libs.mailer.SMTPMailer.send(MailerPlugin.scala:127) ~[play-m
ailer_2.10-3.0.1.jar:3.0.1]
at play.api.libs.mailer.CommonsMailer.send(MailerPlugin.scala:109) ~[pla
y-mailer_2.10-3.0.1.jar:3.0.1]
at play.api.libs.mailer.MailerClient$class.send(MailerPlugin.scala:44) ~
[play-mailer_2.10-3.0.1.jar:3.0.1]
at play.api.libs.mailer.CommonsMailer.send(MailerPlugin.scala:83) ~[play
-mailer_2.10-3.0.1.jar:3.0.1]
at models.utils.Mail$EnvelopJob.run(Mail.java:104) ~[classes/:na]
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40) ~[akka-
actor_2.10-2.3.13.jar:na]
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(Abst
ractDispatcher.scala:397) [akka-actor_2.10-2.3.13.jar:na]
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.g
mail.com, port: 587
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972) ~
[mail-1.4.5.jar:1.4.5]
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:64
2) ~[mail-1.4.5.jar:1.4.5]
at javax.mail.Service.connect(Service.java:317) ~[mail-1.4.5.jar:1.4.5]
at javax.mail.Service.connect(Service.java:176) ~[mail-1.4.5.jar:1.4.5]
at javax.mail.Service.connect(Service.java:125) ~[mail-1.4.5.jar:1.4.5]
at javax.mail.Transport.send0(Transport.java:194) ~[mail-1.4.5.jar:1.4.5
]
at javax.mail.Transport.send(Transport.java:124) ~[mail-1.4.5.jar:1.4.5]
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400) ~[comm
ons-email-1.3.3.jar:1.3.3]
at org.apache.commons.mail.Email.send(Email.java:1437) ~[commons-email-1
.3.3.jar:1.3.3]
at play.api.libs.mailer.CommonsMailer$$anon$2.send(MailerPlugin.scala:10
2) ~[play-mailer_2.10-3.0.1.jar:3.0.1]
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext conne
ction?
at sun.security.ssl.InputRecord.handleUnknownRecord(InputRecord.java:710
) ~[na:1.8.0_101]
at sun.security.ssl.InputRecord.read(InputRecord.java:527) ~[na:1.8.0_10
1]
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973) ~[n
a:1.8.0_101]
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.
java:1375) ~[na:1.8.0_101]
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403
) ~[na:1.8.0_101]
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387
) ~[na:1.8.0_101]
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java
:548) ~[mail-1.4.5.jar:1.4.5]
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:352)
~[mail-1.4.5.jar:1.4.5]
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:207) ~[m
ail-1.4.5.jar:1.4.5]
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938) ~
[mail-1.4.5.jar:1.4.5]
我已經嘗試了不同的組合:
port=587
ssl=false
tls=true
和
port=465
ssl=true
tls=false
但他們都給我同樣的錯誤。
我不能在這裏工作啓用我的Gmail帳戶IMAP或POP3 - 政策並不允許這樣做。
是否有比我在Mail.java類中使用的更好的示例或示例?