JavaMail API and Gmail: Authentication Error 535-5.7.1

I’m having trouble with the JavaMail API and Gmail. When I try to send an email, an authentication error occurs. I am certain that the username and password I am using are correct, and I am using an older Gmail account that might take additional time to set up properly. The error being received is: “535-5.7.1 Username and Password not accepted.”

Below is a revised version of my code:

public class EmailDispatcher {
    private static final String HOST = "smtp.gmail.com";
    private static final String PORT = "465";
    
    public void dispatchEmail() {
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.host", HOST);
        properties.put("mail.smtp.port", PORT);
        
        Session session = Session.getInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("[email protected]", "userpass");
            }
        });
        
        try {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("[email protected]"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
            message.setSubject("Email Test");
            message.setText("This message is a demonstration of a test email.");
            
            Transport.send(message);
        } catch (MessagingException exception) {
            exception.printStackTrace();
        }
    }
}

Has anyone encountered this error before and can suggest what might be causing it?

I’ve dealt with this exact issue before, and it can be quite frustrating. One thing that’s not immediately obvious is that Gmail has become much stricter with its security policies lately. What worked for me was enabling two-factor authentication on my Gmail account, then generating an app-specific password to use in my Java code instead of my regular Gmail password.

Also, double-check your SMTP settings. I notice you’re using port 465, which is for SSL. If you’re using STARTTLS (which your properties suggest), you might want to switch to port 587 instead. Sometimes these small details can make all the difference.

Lastly, if all else fails, consider using OAuth 2.0 for authentication. It’s a bit more complex to set up, but it’s more secure and less likely to run into these kinds of issues in the long run.

I’ve encountered this issue before, and it’s often related to Google’s security measures. Here are a few things to check:

  1. Verify that you’ve allowed less secure apps in your Google account settings.
  2. If you’re using 2-step verification, generate an app password specifically for this application.
  3. Ensure you’re using the correct SMTP settings. For Gmail, try using port 587 with STARTTLS instead of port 465.

If none of these work, consider implementing OAuth 2.0 for authentication. It’s more secure and reliable, though it requires additional setup.

Also, double-check that your account isn’t locked due to suspicious activity. Sometimes Google flags automated login attempts as suspicious, requiring you to verify your account manually.

hey livbrown, i had a similar issue. have u enabled ‘less secure app access’ in ur gmail settings? if not, try that. also, make sure u’re using an app-specific password instead of ur regular gmail password. those steps fixed it for me. hope this helps!