我需要使用Play Mailer(https://github.com/playframework/play-mailer)从Play应用程序发送电子邮件。
在我的GMail帐户中,我启用了双因素身份验证,这是公司政策 - 我无法关闭。所以我使用的密码是我通过我的GMail帐户生成的特定于应用程序的密码(https://support.google.com/mail/answer/185833?hl=en)。
我也无法启用"不太安全的应用程序" (https://support.google.com/accounts/answer/6010255?hl=en)。
我已将此电子邮件和密码值更改为此帖子的通用值。
对于Play Mailer,我在application.conf中有这些参数:
# Mailer
# ~~~~~
play.mailer {
host="smtp.gmail.com"
port=587
ssl=true
tls=true
user="myemail@email.com"
password="mypassword" # This is the application specific generated password
debug=false
mock=false
from="thisemail@email.com"
}
我正在使用此代码发送电子邮件:
/**
* 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: thisemail@email.com
[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: myemail@email.com
但是,当我设置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类中使用的更好的示例或示例?
答案 0 :(得分:1)
看起来就像设置ssl=false
...
我更新的play.mailer配置...
# Mailer
# ~~~~~
play.mailer {
host="smtp.gmail.com"
port=587
ssl=false
tls=true
user="myemail@email.com"
password="mypassword" # This is the application specific generated password
debug=false
mock=false
from="thisemail@email.com"
}