javax.mail.AuthenticationFailedException: failed to connect during Transport.send(Message)

My code to send a mail via gmail was working fine 3 months back. But when I checked it again today, it is failing with below error. My gmail account is not 2 factor authenticated.

Code :

package com.mail;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Mailer {
    
    public static void sendMail(String messageSubject, String messageString)
    {
         Properties props = new Properties();    
         props.put("mail.smtp.host", ConstantsHolder.MAIL_HOST); 
         props.put("mail.smtp.user", ConstantsHolder.FROM_EMAIL);
         props.put("mail.smtp.socketFactory.port", "465");    
         props.put("mail.smtp.socketFactory.class",    
                   "javax.net.ssl.SSLSocketFactory");    
         props.put("mail.smtp.auth", "true");    
         props.put("mail.smtp.port", "465");
         props.put("mail.smtp.debug", "true");
         props.put("mail.smtp.socketFactory.fallback", "false");

         //To use TLS
         props.put("mail.smtp.starttls.enable", "true");
         
         //get Session   
         Session session = Session.getDefaultInstance(props,    
          new Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {    
          return new PasswordAuthentication(ConstantsHolder.FROM_EMAIL,ConstantsHolder.FROM_EMAIL_PASSWD);  
          }    
         });    
         //compose message    
         try {    
          MimeMessage message = new MimeMessage(session);    
          message.addRecipient(Message.RecipientType.TO,new InternetAddress(ConstantsHolder.TO_EMAIL));    
          message.setSubject(messageSubject);    
          message.setText(messageString);    
          //send message  
          Transport.send(message);
          System.out.println("message sent successfully");    
         } 
         catch (MessagingException e) 
         {
             System.out.println("Failed " + e.getMessage()); 
             throw new RuntimeException(e);
         } 
    }
}

Error is :

Failed failed to connect
Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: failed to connect
    at com.mail.Mailer.sendMail(Mailer.java:57)
    at com.mail.MailMain.main(MailMain.java:7)
Caused by: javax.mail.AuthenticationFailedException: failed to connect
    at javax.mail.Service.connect(Service.java:322)
    at javax.mail.Service.connect(Service.java:172)
    at javax.mail.Service.connect(Service.java:121)
    at javax.mail.Transport.send0(Transport.java:190)
    at javax.mail.Transport.send(Transport.java:120)
    at com.mail.Mailer.sendMail(Mailer.java:51)
    ... 1 more

Am I doing something wrong here? I have also checked other question, but its related to "failed to connect : no password provided"

=============================================================== Edit : Solution After trying debug mode ON as asked by @Nasten1988, I found the root cause of the issue and was able to proceed. Hence marking @Nasten1988 's answer as the right answer.

Read my answer for the actual issue I had.

===============================================================

2 answers

  • answered 2021-07-30 21:19 Nasten1988

    Failed to connect: maybe you connection is blocked by firewall, is your software up to date? No password provided: check the requirements from Google, they maybe changed them and check how your password is provided for your mail method. Maybe the debugger can help you. That's what I would look for.

  • answered 2021-07-31 13:20 pramodshetty001

    So this is what helped me -

    I set debug mode to true for the mail session -

    session.setDebug(true);
    

    which displayed the actual problem - Less secure apps were turned off in my gmail account. Apparently if its not used for some time, its auto turned off.

    https://myaccount.google.com/lesssecureapps

    You have to turn off 2-Factor authentication before allowing less secure apps.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum