Search code examples
javaemailjakarta-mail

Getting the "javax.mail.AuthenticationFailedException: failed to connect" Error


What I Want To Do: Send a basic email with the smtp.live.com host to email to someone using a Java Program using this code below:

import java.util.*;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.sun.mail.smtp.SMTPTransport;

public class emailTest {

    public static void main(String[] args) {
        String to = "[email protected]";
        String from = "[email protected]";

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.host", "smtp.live.com");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");


        Session session = Session.getDefaultInstance(props);
        session.setDebug(true);

        try {
            MimeMessage message = new MimeMessage(session);

            message.setFrom(new InternetAddress(from));

            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

            message.setSubject("Java");

            message.setText("Java");

            Transport trans = session.getTransport("smtp");
            trans.connect("smtp.live.com", 587, "[email protected]", "password");
            Transport.send(message);

            System.out.println("Message Sent!");

        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }

}

The Problem: When I try to send the email, I get this error.

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 emailTest.main(emailTest.java:39)

My Question: I just want to know what I'm doing wrong as I've spent a few hours trying to find different ways on how to fix this.


Solution

  • Ok, I soon figured out what I was doing wrong. I can't use Transport.send(message) to send the message.

    Changes Needed: Instead, I have to use trans.sendMessage(message, message.getAllRecipients()).

    Here's the fixed code:

        import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    import com.sun.mail.smtp.SMTPTransport;
    
    public class emailTest {
    
        public static void main(String[] args) {
            String to = "[email protected]";
            String from = "[email protected]";
    
            Properties props = new Properties();
            props.setProperty("mail.transport.protocol", "smtp");
            props.setProperty("mail.host", "smtp.live.com");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
    
    
            Session session = Session.getDefaultInstance(props);
            session.setDebug(true);
    
            try {
                MimeMessage message = new MimeMessage(session);
    
                message.setFrom(new InternetAddress(from));
    
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    
                message.setSubject("Java");
    
                message.setText("Java");
    
                Transport trans = session.getTransport("smtp");
                trans.connect("smtp.live.com", 587, "[email protected]", "password");
                trans.sendMessage(message, message.getAllRecipients());
    
                System.out.println("Message Sent!");
    
            } catch (MessagingException mex) {
                mex.printStackTrace();
            }
        }
    
    }