I’m having trouble setting up email functionality in my Spring application. I want to send emails through Gmail’s SMTP server using JavaMailSenderImpl but keep running into authentication problems.
Even though I’m confident my Gmail credentials are valid, I keep getting a javax.mail.AuthenticationFailedException. I tested the same username and password with a basic Java mail example and it worked perfectly.
What I’ve Tried
I also attempted this updated configuration with additional properties:
check if u missed the setHost() method on the bean. i had this issue where host set only in javaMailProperties wasn’t enough - had to set it directly on JavaMailSenderImpl. also, double-check ur gmail doesn’t have any suspicious activity blocks that might be rejecting ur connection.
Had the exact same issue a few months back and it drove me crazy. The problem isn’t your Spring config - Google killed basic auth for Gmail SMTP in May 2022. You can’t use regular passwords anymore, period. Even with “Less secure app access” enabled, Google’s been phasing that out completely. You need OAuth2 or App Passwords if you’ve got 2FA turned on. Don’t want to mess with OAuth2? Just enable 2FA on your Gmail, generate an App Password for this app, and swap your regular password for that 16-character code in your config. Your XML structure’s fine - just needs the right credentials.
Your config looks right, but there’s probably a protocol mismatch causing the auth failure. I’ve hit this before when Gmail wasn’t handling STARTTLS properly. Add mail.smtp.starttls.required=true to force the secure connection and explicitly set mail.transport.protocol=smtp in your javaMailProperties. Also check if port 587 is blocked - corporate firewalls sometimes mess with SMTP even when your credentials work fine. If you’re still getting auth errors after double-checking your app password, try port 465 with SSL instead of STARTTLS to see if it’s a TLS handshake issue.
This is probably Gmail’s security settings, not your Spring config. Google either wants you to enable “Less secure app access” in your account settings or switch to OAuth2 instead of username/password auth. Your XML looks fine, but Gmail’s gotten way more strict about security lately. Check if you’ve got 2FA turned on - if so, you’ll need an app-specific password. One thing I see missed a lot: make sure you’re setting the host property directly on the JavaMailSenderImpl bean, not just in javaMailProperties. Add <property name="host" value="smtp.gmail.com" /> to your bean definition with your other properties.
Gmail SMTP authentication giving you grief? Same here. Your Spring config isn’t the problem - manual SMTP setup just sucks now with OAuth2 and app passwords.
I spent way too many hours debugging SMTP configs before I found a better way. Skip JavaMailSenderImpl and Gmail’s ever-changing security mess. Just automate the whole thing.
Use a webhook that fires when you need to send emails. Hook it up to Gmail’s API - it handles all the auth automatically. Done. No XML beans, no auth exceptions, no app password headaches.
I run all our production emails this way now. 10-minute setup, then it just works. Better error handling too, plus easy to add templates or conditional logic later.
The platform handles OAuth2 in the background so you only worry about content and triggers. Way more solid than maintaining SMTP configs.
hey! try using an app password from ur google acount if u hv 2FA on. regular passwords might not work for SMTP. go to security settings and generate it, then update ur config! hope this helps!