简体   繁体   中英

Java mail smtp protocol wiht google account in 2023

Well there are a lot of discussion on this topic, but looks like that google enforce its security policy and now, if is it still possible, is hard to find a way to send and mail by the smtp protocol using google

my java application works properly, it is not a fact of code, i use it to send mail by outlook365 or hotmail

the point here is how to configure the properties to pass to the session

Session session = Session.getInstance(prop,auth)

and how to configure the google account to accept an smpt call

to be precise I'm using a google workspace account and not a standard@gmail account

regarding the properties should not be a big doubt, this are listed in several pages of google:

regarding the google account, look like there are 3 strategies: https://support.google.com/a/answer/176600?hl=en

Option 1: Send email with SMTP relay: after configured the routing form my domain this is the log received

mail.smtp.starttls.enable=true
mail.smtp.port=587
mail.smtp.auth=true
mail.smtp.host=smtp-relay.gmail.com
mail.smtp.ssl.protocols=TLSv1.2
mail.smtp.ssl.trust=smtp-relay.gmail.com --> in this way a consider valid their certificate

but go head...

DEBUG SMTP: protocolConnect login, host=smtp-relay.gmail.com, user=myuser@my.domain, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed

Caused by: javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials

Option 2: Send email with the Gmail SMTP server tested but this is no more possible, google removed in the 2022 the creation of app password

Option 3: Send email with the restricted Gmail SMTP server not valid approach, you can send only mail in your organization

if sameone succed could be nice to hear

Apps passwords can be used to login to googles smtp server you need to have 2fa enabled on your google account in order to create an apps password.

Simply create an apps password and use it in place of your standard gmail password in your code.

Another option would be to use XOauth2 and authorize a user to access their gmail account. This can be used if you cant enable 2fa on the account.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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