openshift上java邮件程序中的身份验证错误

时间:2014-12-31 12:50:57

标签: email authentication gmail javamail openshift

我正在尝试使用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公开其API以将邮件作为REST服务发送,而不是使用Gmail SMTP。对于可以使用免费帐户发送邮件数量上限的开发者,它是免费的。它在Openshift上完美运行。这里我创建了一个使用Jersey 2.x使用Mailgun服务的示例。 https://gist.github.com/chhabraromit/0c0a16545dd31f471332

2 个答案:

答案 0 :(得分:6)

原因:Google阻止了来自未知位置(生产中的应用)的访问权限

解决方案:转到http://www.google.com/accounts/DisplayUnlockCaptcha并点击继续(这将为注册新应用授予10分钟的访问权限)。在此之后我的生产应用程序开始发送电子邮件;)

答案 1 :(得分:1)

Gmail确实希望您从运行应用程序的同一台计算机上的浏览器进行登录。当您的应用程序在openshift上运行时,这是不可能的。

您可以尝试在运行浏览器的同一台计算机上运行您的应用程序,看看是否会说服您的应用程序也可以从其他计算机上登录。