'535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Tenant

I am sending e email using an SMTP error . I am getting Authentication unsuccessful. The username and password are correct. Am I doing something wrong.

public class Office365TextMsgSend {

Properties properties;
Session session;
MimeMessage mimeMessage;

String USERNAME = "[email protected]";
String PASSWORD = "xxxxxxx";
String HOSTNAME = "smtp.office365.com";
String STARTTLS_PORT = "587";
boolean STARTTLS = true;
boolean AUTH = true;
String FromAddress="[email protected]";

public static void main(String args[]) throws MessagingException {
    String EmailSubject = "Subject:Text Subject";
    String EmailBody = "Text Message Body: Hello World";
    String ToAddress = "[email protected]";
    Office365TextMsgSend office365TextMsgSend = new Office365TextMsgSend();
    office365TextMsgSend.sendGmail(EmailSubject, EmailBody, ToAddress);
}

public void sendGmail(String EmailSubject, String EmailBody, String ToAddress) {
    try {
        properties = new Properties();
        properties.put("mail.smtp.host", HOSTNAME);
        // Setting STARTTLS_PORT
        properties.put("mail.smtp.port", STARTTLS_PORT);
        // AUTH enabled
        properties.put("mail.smtp.auth", AUTH);
        // STARTTLS enabled
        properties.put("mail.smtp.starttls.enable", STARTTLS);
        properties.put("mail.smtp.ssl.protocols", "TLSv1.2");
        // Authenticating
        Authenticator auth = new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(USERNAME, PASSWORD);
            }
        };

        // creating session
        session = Session.getInstance(properties, auth);

        // create mimemessage
        mimeMessage = new MimeMessage(session);
        
        //from address should exist in the domain
        mimeMessage.setFrom(new InternetAddress(FromAddress));
        mimeMessage.addRecipient(RecipientType.TO, new InternetAddress(ToAddress));
        mimeMessage.setSubject(EmailSubject);

        // setting text message body
        mimeMessage.setText(EmailBody);

        // sending mail
        Transport.send(mimeMessage);
        System.out.println("Mail Send Successfully");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

Error:

javax.mail.AuthenticationFailedException: 535 5.7.139 Authentication unsuccessful, SmtpClientAuthentication is disabled for the Tenant. Visit https://aka.ms/smtp_auth_disabled for more information. [MA1PR01CA0169.INDPRD01.PROD.OUTLOOK.COM]



Solution 1:[1]

As the error explicitly states, SMTP authentication is disabled. It even provides you with a very helpful link to https://aka.ms/smtp_auth_disabled. The link explains how to enable SMTP AUTH for the whole organization or only for some mailboxes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitry Streblechenko