【发布时间】:2022-11-22 07:01:53
【问题描述】:
我正在使用 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();
}
}
}
错误:
javax.mail.AuthenticationFailedException:535 5.7.139 身份验证 不成功,为租户禁用 SmtpClientAuthentication。 访问https://aka.ms/smtp_auth_disabled获取更多信息。 [MA1PR01CA0169.INDPRD01.PROD.OUTLOOK.COM]
【问题讨论】:
-
我有同样的问题,我得到的“第一个”答案是“在仪表板管理员上禁用/启用它”。但是我正在检查是否可以在没有管理员更改的情况下使用它。
标签: java outlook office365 exchange-server