Javamail with Gmail: 535-5.7.1 Username and Password Not Accepted

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.
Sample Image

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.

Sample Image

Then type in WP Mail SMTP or another name that will help you identify this password in the space provided.

Sample Image

After that, you just need to click the Generate button to create your password.

Sample Image

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



Leave a reply



Submit