JavaMail with Gmail: 535-5.7.1 Username and Password not accepted
The given code snippet works fine on my Gmail account, so this problem lies somewhere else. Did you follow the link given in the error message? It contains the following hints:
- Make sure that you've entered your full email address (e.g. username@gmail.com)
- Re-enter your password to ensure that it's correct. Keep in mind that passwords are case-sensitive.
- Make sure your mail client isn't set to check for new mail too often. If your mail client checks for new messages more than once every 10 minutes, your client might repeatedly request your username and password.
Especially the last point is important. Google is very strict in this. If you're trying to connect Gmail for example more than 10 times in a minute programmatically, then you may already get blocked. Have a bit of patience, after some time it will get unblocked.
If you'd like more freedom in sending mails, I recommend to look for a dedicated mail host or to setup your own mail server, such as Apache James or Microsoft Exchange. I've already answered this in detail in one of your previous questions.
JavaMail API : Username and Password not accepted (Gmail)
To be able send mail messages via your gmail account, you should allow unsecure applications (which your application is by gmail's point of view) in google account security settings.
UPD:
Also, if you want to see debug messages, use next java mail property:
props.put("mail.debug", "true");
It can help you to find out what happening behind the scenes.
javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted. Learn more at
Google has announced that it’s disabling the Less Secure Apps feature on some Google accounts from May 30th, 2022.
To be able to send messages through your gmail account, you can set up an app password in your Google account.
An app password works like an alternate password for your account. It can only be used by the applications you share it with, so it’s more secure than sharing your primary password
If you’d like to use an app password,you’ll need to turn on two factor authentication for your Google account . After that, you can create an app specific password for WP Mail SMTP in the security settings for your Google account.
To do so, select Other (Custom name) as the app on the App passwords screen.
Then type in WP Mail SMTP or another name that will help you identify this password in the space provided.
After that, you just need to click the Generate button to create your password.
Note: any time you change your main Google account password, it will deactivate your app passwords as well. If you change your password frequently, this may not be the best workaround.
After generating password, you put your generated password in password property of your property file:
mail:
host: smtp.gmail.com
username: mail@gmail.com
password: <GENERATED_PASSWORD>
port: 465
protocol: smtps
Related Topics
Sharing a Persistence Unit Across Components in a .Ear File
Resize a Picture to Fit a Jlabel
Sessiontimeout: Web.Xml VS Session.Maxinactiveinterval()
Java:Does Wait() Release Lock from Synchronized Block
Why Are Java 8 Lambdas Invoked Using Invokedynamic
Comparing Boxed Long Values 127 and 128
"Noclassdeffounderror: Could Not Initialize Class" Error
JPA - Returning an Auto Generated Id After Persist()
What Is the Idiomatic Way to Compose a Url or Uri in Java
Handling Datetime Values 0000-00-00 00:00:00 in Jdbc
Is It Bad Practice to Use Reflection in Unit Testing
Are Java Static Calls More or Less Expensive Than Non-Static Calls
How to Solve the "A Generic Array of T Is Created for a Varargs Parameter" Compiler Warning
When to Use a Constructor and When to Use Getinstance() Method (Static Factory Methods)