简体   繁体   English

OpenShift上Java邮件程序中的身份验证错误

[英]Authentication error in java mail program on openshift

I am trying to send mails using Java Mail API & Gmail SMTP server. 我正在尝试使用Java Mail API和Gmail SMTP服务器发送邮件。 Here is my code: 这是我的代码:

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);
}

It is running perfectly on local but giving AuthenticationFailedException on Openshift. 它在本地运行良好,但在Openshift上提供了AuthenticationFailedException。 Below is the openshift error log. 以下是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)

I have tried all the answers given at Java Mail fails to authenticate smtp setting in openshift server . 我已经尝试过Java Mail给出的所有答案都无法验证openshift服务器中的smtp设置 I have tried logging into the gmail account and then sending the mail through my app in the same browser but still its not working. 我尝试登录gmail帐户,然后在同一浏览器中通过我的应用发送邮件,但仍然无法正常工作。 Dual verification is not activated in the gmail account & the account is not protected by modern security standards and hence it allows less secure apps to connect (according to gmail). gmail帐户中未激活双重验证,并且该帐户不受现代安全标准的保护,因此,它允许安全性较低的应用程序进行连接(根据gmail)。

Alternate Solution : Instead of Gmail SMTP, I tried Mailgun which exposes its API to send mail as REST services. 替代解决方案 :我尝试使用Mailgun而不是Gmail SMTP,该Mailgun公开其API以REST服务形式发送邮件。 It is free for developers with an upper limit on the number of mails which can be sent using a free account. 对于使用免费帐户可以发送的邮件数量有上限的开发人员来说,它是免费的。 It is working perfectly on Openshift. 它在Openshift上运行完美。 Here I have created an example to use Mailgun service using Jersey 2.x. 在这里,我创建了一个使用Jersey 2.x使用Mailgun服务的示例。 https://gist.github.com/chhabraromit/0c0a16545dd31f471332 https://gist.github.com/chhabraromit/0c0a16545dd31f471332

Reason: Google was blocking access from unknown location (app in production) 原因: Google阻止了来自未知位置的访问(生产中的应用)

Solution: Go to http://www.google.com/accounts/DisplayUnlockCaptcha and click continue (this will grant access for 10 minutes for registering new apps). 解决方案:访问http://www.google.com/accounts/DisplayUnlockCaptcha并单击“继续”(这将授予10分钟的访问权,用于注册新应用)。 After this my app in production started sending emails ;) 此后,我在生产中的应用程序开始发送电子邮件;)

Gmail really wants you to login from a browser on the same machine that your application is running on. Gmail确实希望您从运行应用程序的同一台计算机上的浏览器登录。 When your application is running on openshift, that's not possible. 当您的应用程序在openshift上运行时,这是不可能的。

You can try running your application on the same machine that your browser is running on, to see if that will convince Gmail that it's ok for your application to login from some other machine as well. 您可以尝试在运行浏览器的同一台计算机上运行应用程序,以查看是否可以使Gmail确信您的应用程序也可以从其他计算机登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM