【发布时间】:2020-08-07 22:24:43
【问题描述】:
您好,我目前正在开发 java 应用程序,它将通过 Java Mail Api(1.6.2) 向多个收件人发送邮件,我已经根据 Microsoft docs 配置了 SMTP 问题是代码正在使用我的个人 hotmail 电子邮件 ID,但是公司办公室 365 帐户失败。
错误:javax.mail.AuthenticationFailedException: 535 5.7.3 身份验证不成功 [PN1PR0101CA0066.INDPRD01.PROD.OUTLOOK.COM]
POP 和 IMAP 正在工作(接收邮件),我可以在 Office 365 Web 中使用密码登录,我也尝试过更改密码。
代码:
User user = Credentials.ACC;
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.office365.com");//outlook.office365.com
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");//25
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.transport.protocol", "smtp");
//props.put("mail.smtp.ssl.enable", true);
Session session = Session.getInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user.getUsername(), user.getPassword());
}
});
session.setDebug(true);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(user.getUsername());
msg.setRecipients(Message.RecipientType.TO,
"some@mail.com");
msg.setSubject("Testing SMTP using [" + user.getUsername() + "]");
msg.setSentDate(new Date());
msg.setText("Hey, this is a test from [" + user.getUsername() + "], Sending via Java Mail API");
Transport.send(msg);
System.out.println("Sent Ok");
} catch (MessagingException e) {
System.out.println("Something went wrong");
e.printStackTrace();
}
【问题讨论】:
-
首先通过修复这些common mistakes 来简化您的代码。您是否使用与 Web UI 相同的用户名?可以配置 Thunderbird 使用相同的用户名和密码登录吗?
-
是的,我使用与网络相同的用户名,而在 Thunderbird 中,它提供了 SSL/TLS 错误
标签: java outlook smtp office365 jakarta-mail