我使用 SMTP 发送电子邮件时出现错误。我的身份验证失败。用户名和密码正确。我是不是做错了什么?
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();
}
}
}
错误:
javax.mail.AuthenticationFailedException:535 5.7.139 身份验证 如果不成功,租户的 SmtpClientAuthentication 将被禁用。 请访问 https://aka.ms/smtp_auth_disabled 了解更多信息。 [MA1PR01CA0169.INDPRD01.PROD.OUTLOOK.COM]
正如错误明确指出的那样,SMTP 身份验证已禁用。它甚至为您提供了一个非常有用的链接:https://aka.ms/smtp_auth_disabled。该链接解释了如何为整个组织或仅为某些邮箱启用 SMTP AUTH。
AAD 安全默认值可能会阻止此操作。我有:
Get-TransportConfig | Format-List SmtpClientAuthenticationDisabled
SmtpClientAuthenticationDisabled : False
但是还是得到535 5.7.139认证失败
事实证明,问题出在 AAD 中的安全默认值
关闭 Azure Active Directory 中的安全默认值
Start by logging into the Azure Active Directory (https://aad.portal.azure.com/).
Select Azure Active Directory
In left menu click: Azure Active Directory
Select Properties from the menu under Manage
AAD properties
Select Manage security defaults at the very bottom of the properties page
Managing security defaults
Move the slider to No and click Save
save security defaults
您可以检查:
Next login to or navigate to the Microsoft 365 Admin Center https://admin.microsoft.com/
Select Settings > Org Settings
Under Services, select Modern Authentication
Ensure Authentication SMTP is checked
SMTP 服务器需要安全连接,或者客户端未经过身份验证。服务器响应为:5.7.57 客户端未经过身份验证,无法发送邮件。错误:535 5.7.139 身份验证失败,租户的 SmtpClientAuthentication 已禁用。请访问 https://aka.ms/smtp_auth_disabled 了解更多信息。 [PN3PR01CA0064.INDPRD01.PROD.OUTLOOK.COM 2023-09-19T15:34:13.757Z 08DBB8938442C11E]- plesk
任何人都知道这个解决方案,我从 plesk 收到此错误