535 5.7.139 身份验证失败,租户禁用 SmtpClientAuthentication

asp*_*nty 4 java outlook exchange-server office365

我使用 SMTP 发送电子邮件时出现错误。我的身份验证失败。用户名和密码正确。难道我做错了什么。

public class Office365TextMsgSend {

Properties properties;
Session session;
MimeMessage mimeMessage;

String USERNAME = "xxxx@xxxx.xx";
String PASSWORD = "xxxxxxx";
String HOSTNAME = "smtp.office365.com";
String STARTTLS_PORT = "587";
boolean STARTTLS = true;
boolean AUTH = true;
String FromAddress="xxxx@xxxx.xx";

public static void main(String args[]) throws MessagingException {
    String EmailSubject = "Subject:Text Subject";
    String EmailBody = "Text Message Body: Hello World";
    String ToAddress = "xxxxxx@gmail.com";
    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();
    }
}

}
Run Code Online (Sandbox Code Playgroud)

错误:

javax.mail.AuthenticationFailedException: 535 5.7.139 身份验证不成功,租户的 SmtpClientAuthentication 被禁用。请访问https://aka.ms/smtp_auth_disabled了解更多信息。[MA1PR01CA0169.INDPRD01.PROD.OUTLOOK.COM]

Dmi*_*nko 6

正如错误明确指出的那样,SMTP 身份验证已禁用。它甚至为您提供了一个非常有用的链接https://aka.ms/smtp_auth_disabled。该链接解释了如何为整个组织或仅为某些邮箱启用 SMTP AUTH。