Spring Boot email service failing to authenticate with Gmail

I’m having trouble with my Spring Boot app’s email service. It’s not connecting to Gmail even though I’m using an app password. Here’s what’s happening:

When I try to send an email, I get this error:

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

I’ve double-checked my config:

spring:
  mail:
    host: smtp.gmail.com
    port: 587
    username: ${GMAIL_USERNAME}
    password: ${GMAIL_PASSWORD}
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

My email service looks like this:

@Service
public class EmailSender {
    @Autowired
    private JavaMailSender mailClient;
    @Value("${spring.mail.username}")
    private String sender;

    @Async
    public void deliverEmail(String recipient, String topic, String body) {
        SimpleMailMessage email = new SimpleMailMessage();
        email.setFrom(sender);
        email.setTo(recipient);
        email.setSubject(topic);
        email.setText(body);
        mailClient.send(email);
    }
}

I’ve tried creating new app passwords, but no luck. Any ideas what might be wrong?

I’ve faced similar issues with Gmail authentication in Spring Boot. One thing that worked for me was explicitly setting the mail.smtp.ssl.trust property. Try adding this to your configuration:

spring:
mail:
properties:
mail:
smtp:
ssl:
trust: smtp.gmail.com

Also, ensure you’re using your full email address as the username, not just the part before @gmail.com. If that doesn’t help, you might want to check if your Google Account has 2-Step Verification enabled. If it is, you’ll need to use an app-specific password instead of your regular account password.

As a last resort, you could try switching to port 465 and enabling SSL instead of TLS. Some users have reported success with this approach when dealing with Gmail authentication issues in Spring Boot applications.

hey noah, have u tried using a different email provider? sometimes gmail can be a pain. maybe try outlook or something? also, double-check ur app password - they can expire. if all else fails, u could try setting up a local smtp server for testing. good luck!

Have you considered checking your Gmail account settings? Sometimes, Gmail’s security features can interfere with third-party app access. Try enabling ‘Less secure app access’ in your Google Account settings, though this isn’t recommended for long-term use.

Another possibility is that your environment variables for GMAIL_USERNAME and GMAIL_PASSWORD aren’t set correctly. Double-check these in your application.properties or application.yml file.

If those don’t work, you might want to try using OAuth2 authentication instead of app passwords. It’s more secure and less likely to be blocked by Gmail’s security measures. Spring Boot has good support for OAuth2, and while it requires more setup, it’s generally more reliable for production use.

Lastly, ensure you’re using the latest version of the Jakarta Mail API. Older versions might have compatibility issues with recent Gmail changes.