Search code examples
javagmailjakarta-mail

Java mail API and Gmail


I know it was possible before, but nowadays gmail causes exception when trying to use Java mail API in various ways to send email messages with a gmail account. Gmail has its own mail API which requires oauth2.0 authentication. Is there any way to automatically send emails with a gmail account using Java?

This is the exception I get:

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)

Solution

  • This is the solution:

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

    Less secure application access is the only way apparently.