1

I am trying to send an email in a java application calling this method:

public static void sendEmail() {

    // Create object of Property file
    Properties props = new Properties();

    // this will set host of server- you can change based on your requirement 
    props.put("mail.smtp.host", "smtp.office365.com");

    // set the port of socket factory 
    //props.put("mail.smtp.socketFactory.port", "587");

    // set socket factory
    //props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

    // set the authentication to true
    props.put("mail.smtp.auth", "true");

    // set the port of SMTP server
    props.put("mail.smtp.port", "587");

    // This will handle the complete authentication
    Session session = Session.getDefaultInstance(props,

            new javax.mail.Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {

                return new PasswordAuthentication("xx@mail.com", "xx");


                }

            });

    try {

        // Create object of MimeMessage class
        Message message = new MimeMessage(session);

        // Set the from address
        message.setFrom(new InternetAddress("xx@mail.com"));

        // Set the recipient address
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("yy@mail.com"));

                    // Add the subject link
        message.setSubject("Testing Subject");

        // Create object to add multimedia type content
        BodyPart messageBodyPart1 = new MimeBodyPart();

        // Set the body of email
        messageBodyPart1.setText("This is message body");

        // Create object of MimeMultipart class
        Multipart multipart = new MimeMultipart();

        // add body part 2
        multipart.addBodyPart(messageBodyPart1);

        // set the content
        message.setContent(multipart);

        // finally send the email
        Transport.send(message);

        System.out.println("=====Email Sent=====");

    } catch (MessagingException e) {

        throw new RuntimeException(e);

    }

}

but for some reason when the debug hits Transport.send(), I got this exception:

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [DB6PR0802CA0037.eurprd08.prod.outlook.com]

why is this happening even though I used the Authenticator()?

eeadev
  • 3,662
  • 8
  • 47
  • 100

1 Answers1

6

If you connect via port 587 you initially start a plain connection where you have to start TLS by explicitly sending the STARTTLS-command. You have to tell JavaMail to do that, otherwise it will try to proceed unsecured. The SMTP-server doesn't send any authentication-mechanism-informations unless the TLS-connection is established, so JavaMail is assuming that no authentication is needed and tries to send the mail without.

Add the following entry to the properties:

props.put("mail.smtp.starttls.enable", "true");

WIth that JavaMail should try to switch to TLS before trying to authenticate.

If that fails, you need to enable debugging by setting the property

props.put("mail.debug", "true");

and post the output here.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
Lothar
  • 5,323
  • 1
  • 11
  • 27
  • Also, fix these [common JavaMail mistakes](https://javaee.github.io/javamail/FAQ#commonmistakes) and get rid of the Authenticator. – Bill Shannon Feb 22 '18 at 18:15
  • thank you @Lothar, this props.put("mail.smtp.starttls.enable", "true"); solved the problem! – eeadev Feb 23 '18 at 09:33