Search code examples
javaemailjakarta-mail

Javamail API exception - Authentication required


I am using following code to send mail from

public class Test {

    private String SMTP_HOST_NAME = "smtp.bizmail.yahoo.com";
    private String SMTP_AUTH_USER = "[email protected]";
    private String SMTP_AUTH_PWD = "mypassword";
    private String SMTP_MAIL_PORT = "587";  

    public void postMail(String recipient, String subject, String message)
    {
        Properties props = new Properties();

        SMTPAuthenticator authenticator = new SMTPAuthenticator();
        props.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.host", SMTP_HOST_NAME);
        props.setProperty("mail.smtp.port", SMTP_MAIL_PORT);
        props.setProperty("mail.smtp.starttls.enable","true");
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.debug", "true");

        Session session = Session.getInstance(props, authenticator);
        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(SMTP_AUTH_USER));
            msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient));
            msg.setSubject(subject);
            msg.setContent(message, "text/html");
            Transport.send(msg);
            System.out.println("Email send successfully Done");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
            String username = SMTP_AUTH_USER;
            String password = SMTP_AUTH_PWD;
            return new PasswordAuthentication(username, password);
        }
    }

    public static void main(String[] args) {
        Test s = new Test();
        s.postMail("[email protected]", "Test Subject", "Test Mail");
    }
}     

From above code i am able to send email successfully, But now i am getting exception

javax.mail.MessagingException: 530 5.7.1 Authentication required 

Is there any changes in Yahoo smtp server or something else

Here is stackstrace

DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG: SMTPTransport trying to connect to host "smtp.bizmail.yahoo.com", port 587
DEBUG SMTP RCVD: 220 smtp.bizmail.yahoo.com ESMTP ready
DEBUG: SMTPTransport connected to host "smtp.bizmail.yahoo.com", port: 587
DEBUG SMTP SENT: EHLO ET-3-PC
DEBUG SMTP RCVD: 250-smtp.bizmail.yahoo.com
250-PIPELINING
250-SIZE 41697280
250-8 BITMIME
250 STARTTLS

DEBUG SMTP Found extension "PIPELINING", arg ""
DEBUG SMTP Found extension "SIZE", arg "41697280"
DEBUG SMTP Found extension "8", arg "BITMIME"
DEBUG SMTP Found extension "STARTTLS", arg ""
DEBUG SMTP: use8bit false
DEBUG SMTP SENT: MAIL FROM:<[email protected]>
DEBUG SMTP RCVD: 530 5.7.1 Authentication required

DEBUG SMTP SENT: QUIT
javax.mail.SendFailedException: Sending failed;
  nested exception is: 
    javax.mail.MessagingException: 530 5.7.1 Authentication required

    at javax.mail.Transport.send0(Transport.java:219)
    at javax.mail.Transport.send(Transport.java:81)
    at com.einsteiner.util.SendMailToUser.postMail(SendMailToUser.java:67)
    at com.einsteiner.util.SendMailToUser.main(SendMailToUser.java:197)

Solution

  • Based on the debug output, you're using a very old version of JavaMail. That's why the properties you're setting aren't working.

    You should upgrade to a newer version (at least 1.4.7, which supports these properties). You can find the latest version here.