1

Unable to send mail to gmail account using Java program, it's giving AuthenticationFailedException root cause:

javax.mail.SendFailedException: Sending failed;
  nested exception is:
    class javax.mail.AuthenticationFailedException
    at javax.mail.Transport.send0(Transport.java:218)
    at javax.mail.Transport.send(Transport.java:80)
    at com.sekhar.mail.SendMail.<init>(SendMail.java:32)
    at com.sekhar.mail.SendMail.main(SendMail.java:48)

Check the program here:

public class SendMail {

    public SendMail() {
        // TODO Auto-generated constructor stub
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

        try {
            Authenticator auth = new SMTPAuthenticator();

            Session session = Session.getInstance(props,auth);
            MimeMessage msg = new MimeMessage(session);
            msg.setSubject("Open");
            msg.setFrom(new InternetAddress("***@gmail.com"));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress("***@gmail.com"));
            msg.setText("How are you");
            Transport.send(msg);
            System.out.println("Mail Delivered......");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    private class SMTPAuthenticator extends Authenticator{
        public PasswordAuthentication getPasswordAuthentication(){
            return new PasswordAuthentication("***@gmail.com","****");
        }
    }
    public static void main(String[] args) {

        SendMail mail = new SendMail();

    }

} 
Alex Riley
  • 169,130
  • 45
  • 262
  • 238

1 Answers1

0

Take a look at this page. It has a full example you can change to fit your needs. Also try seeing if the problem would be solved by this question.

On the first link, it show s that the port should be 587, not 465. Both links used ttls with props.put("mail.smtp.starttls.enable", "true");.

Hope this helped.

Community
  • 1
  • 1
Sheikh
  • 539
  • 1
  • 7
  • 29