【发布时间】:2015-02-27 13:26:29
【问题描述】:
我正在尝试使用 Java Mail API 和 Gmail SMTP 服务器发送邮件。这是我的代码:
final String username = "myusername@gmail.com";
final String password = "mypassword";
Properties props = new Properties();
props.put("mail.smtp.user", username);
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.EnableSSL.enable", "true");
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(clientEmail));
message.setSubject(subject);
message.setText("Dear "+clientName+",\n\n "+body);
Transport.send(message);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
它在本地运行完美,但在 Openshift 上出现 AuthenticationFailedException。以下是 openshift 错误日志。
DEBUG: JavaMail version 1.4.7
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Orcle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle],com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false 220 mx.google.com ESMTP k9sm38376122qaj.7 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 465 EHLO ex-std-node454.prod.rhcloud.com 250-mx.google.com at your service, [54.166.173.168] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENT TOKEN"
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
DEBUG SMTP: STARTTLS requested but already using SSL
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
http://mooark-romitchhabra.rhcloud.com/mooark/ADP/Thanks.jsp
DEBUG SMTP: AUTH LOGIN failed
Exception in thread "Thread-4" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvrd
534-5.7.14 sdnRpIjv6joT7_WeGn1AAxAwQL6nET1nfNhi7oUeKqgdxHVmsc5XgZ4UFD26KJ0PfVSnLO
534-5.7.14 m6C8BUAB5Xk3Yp0L5Is0c0mUcP5MQMA4exuzGXiaBe0zPxMtfRLPz_y8shSO0WwDwjsV6D
534-5.7.14 ZP5fRWHRtbMDoI0hx81GBM59dc7tAM4nSsbTl1J3MoQfzeaK3_WYZ-BLtUl28P-qgcSkd
534-5.7.14 eZMQKxw> Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 k9sm38376122qaj.7 - gsmtp
at com.adp.thread.SendMail.run(SendMail.java:89)
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvrd
534-5.7.14 sdnRpIjv6joT7_WeGn1AAxAwQL6nET1nfNhi7oUeKqgdxHVmsc5XgZ4UFD26KJ0PfVSnLO
534-5.7.14 m6C8BUAB5Xk3Yp0L5Is0c0mUcP5MQMA4exuzGXiaBe0zPxMtfRLPz_y8shSO0WwDwjsV6D
534-5.7.14 ZP5fRWHRtbMDoI0hx81GBM59dc7tAM4nSsbTl1J3MoQfzeaK3_WYZ-BLtUl28P-qgcSkdh
534-5.7.14 eZMQKxw> Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 k9sm38376122qaj.7 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
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 com.adp.thread.SendMail.run(SendMail.java:85)
我已经尝试了Java Mail fails to authenticate smtp setting in openshift server 给出的所有答案。我尝试登录 gmail 帐户,然后在同一个浏览器中通过我的应用程序发送邮件,但仍然无法正常工作。双重验证未在 gmail 帐户中激活,并且该帐户不受现代安全标准的保护,因此它允许连接不太安全的应用程序(根据 gmail)。
替代解决方案:我尝试使用 Mailgun 代替 Gmail SMTP,它公开其 API 以将邮件作为 REST 服务发送。对于可以使用免费帐户发送的邮件数量有上限的开发人员来说,它是免费的。它在 Openshift 上运行良好。在这里,我创建了一个使用 Jersey 2.x 使用 Mailgun 服务的示例。 https://gist.github.com/chhabraromit/0c0a16545dd31f471332
【问题讨论】:
标签: email authentication gmail jakarta-mail openshift