Skip to content
Advertisement

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.

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement