Java邮件API和Gmail

时间:2016-06-01 18:54:51

标签: java gmail javamail

我之前知道这是可能的,但是现在gmail在尝试以各种方式使用Java邮件API来发送带有Gmail帐户的电子邮件时会导致异常。 Gmail有自己的邮件API,需要oauth2.0身份验证。有没有办法使用Java自动发送带有Gmail帐户的电子邮件?

这是我得到的例外:

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbuG
534-5.7.14 pnCDx7bYqYiAp9mjb1DPP3FApa-8RaN63YhQPOnG79CDnHBYQSBXvL6SGxPytaFyxSH4Qc
534-5.7.14 q0QS2LfefCQCG-PQs_bLaR6eCmAzFz1FqjKRS8JWyL9Hsa-8d1Hh1x_1cpRuqhVAC5Smg7
534-5.7.14 IOpErfFwkYr76t7GkEGbXgpxs2ZZyYP83Xi2ZWC06x2fWw8SOZPL9p5fO30TZw8lnTSpew
534-5.7.14 Kb8J0ODoPvSSoMtlQJp1NO65GsgxE> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 r76sm5990846wme.14 - gsmtp

at TestGmail.main(TestGmail.java:75)
Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14     <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbuG
534-5.7.14 pnCDx7bYqYiAp9mjb1DPP3FApa- 8RaN63YhQPOnG79CDnHBYQSBXvL6SGxPytaFyxSH4Qc
534-5.7.14 q0QS2LfefCQCG-PQs_bLaR6eCmAzFz1FqjKRS8JWyL9Hsa-  8d1Hh1x_1cpRuqhVAC5Smg7
534-5.7.14   IOpErfFwkYr76t7GkEGbXgpxs2ZZyYP83Xi2ZWC06x2fWw8SOZPL9p5fO30TZw8lnTSpew
534-5.7.14 Kb8J0ODoPvSSoMtlQJp1NO65GsgxE> Please log in via your web   browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754   r76sm5990846wme.14 - gsmtp

at   com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport. java:914)
at   com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:825)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:730)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at TestGmail.main(TestGmail.java:70)

1 个答案:

答案 0 :(得分:1)

这是解决方案:

https://www.google.com/settings/security/lesssecureapps

安全性较低的应用程序访问显然是唯一的方法。