eeadev
eeadev

Reputation: 3852

Client was not authenticated to send anonymous mail during MAIL FROM

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("[email protected]", "xx");


                }

            });

    try {

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

        // Set the from address
        message.setFrom(new InternetAddress("[email protected]"));

        // Set the recipient address
        message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("[email protected]"));

                    // 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()?

Upvotes: 1

Views: 10452

Answers (1)

Lothar
Lothar

Reputation: 5459

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.

Upvotes: 6

Related Questions