我正在尝试使用Java从Outlook企业邮件帐户发送邮件。 我在StackOverflow中发现了很少的解决方案,但这并没有真正的帮助。
这是我的Java代码
String host = "smtp.office365.com";
final String user = "my_corporate_username";
final String pass = "my_corporate_password";
props.put("mail.smtp.user", user);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
Authenticator auth = new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass);
}
};
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText("Hey, this is the testing email.");
msg.setSubject("Testing");
msg.setFrom(new InternetAddress(from_mail_id));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to_mail_id));
Transport.send(msg);
输出为
javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [BM1PR01CA0077.INDPRD01.PROD.OUTLOOK.COM]
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:914)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:825)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:730)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at Main.sendMail(Main.java:109)
at Main.main(Main.java:28)
当我使用我的个人Outlook帐户时,以上代码有效。该错误可能是由于某些身份验证因素而发生的。
有关如何解决此问题的任何建议。
谢谢。