Java email sending fails with 535-5.7.8 authentication error using Gmail SMTP

I’m getting an authentication error when trying to send emails through Gmail SMTP in my Java program. My Gmail account has two-factor authentication enabled and I created an application-specific password for this purpose.

When I run my email sending code, I keep getting this error:

javax.mail.AuthenticationFailedException: 535-5.7.8 Username and Password not accepted

I’ve verified my login credentials several times and they seem correct. The app password was generated properly from my Google account settings. I’m not sure if this is a configuration problem on my end or something else entirely.

Has anyone encountered this issue before? What other settings should I check to troubleshoot this Gmail SMTP authentication problem?

yh, I had this too. you might wanna look into ur less secure app access setting, even with an app password sometimes it’s blocked. also don’t forget to check if ur smtp port is right, 587 with starttls usually works. good luck!

I encountered the same authentication issue last month and it turned out to be related to Google’s updated security policies. Even though you generated an app password correctly, Gmail sometimes requires you to enable IMAP access in your account settings before SMTP will work properly. Check your Gmail settings under Forwarding and POP/IMAP and make sure IMAP is enabled. Additionally, verify that your JavaMail configuration is using the correct authentication mechanism - I had to explicitly set the mail.smtp.auth property to true and ensure I was using the app password, not your regular account password. The timing of when you generated the app password can also matter, as there’s sometimes a brief delay before it becomes active across all Google services.

This error can be particularly frustrating because the app password generation process seems straightforward but there are several hidden gotchas. One thing that caught me off guard was that Gmail requires you to use your full email address as the username in your SMTP configuration, not just the part before the @ symbol. I spent hours debugging before realizing this simple mistake. Another issue I encountered was that some older JavaMail versions have compatibility problems with Gmail’s current security requirements. Make sure you’re using a recent version of the javax.mail library, preferably 1.6.2 or newer. Also worth checking your firewall settings - corporate networks sometimes block outbound SMTP traffic on port 587, which would cause authentication to fail even with correct credentials. Try testing your connection from a different network to rule this out.