【发布时间】:2017-08-24 02:32:25
【问题描述】:
我正在尝试使用我的应用程序中的 Java Mail API 发送电子邮件。我使用的代码如下:
private void sendMail(Properties props, String fromEmail, String password, String toEmail, String messageSubject, String messageBody)
{
final String username = fromEmail;
final String pasword = password;
//props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username,pasword);
}
});
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toEmail));
message.setSubject(messageSubject);
message.setText(messageBody);
Transport.send(message);
}
catch (MessagingException e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
我有以下属性:
{mail.smtp.port=587, mail.smtp.socketFactory.port=465, mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory, mail.smtp.auth=true, mail.smtp.host=smtp.office365.com}
我收到以下异常:
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
相同的代码适用于smtp.gmail.com,我能够从基于 gmail 的电子邮件发送邮件。这个问题有解决办法吗?
【问题讨论】:
-
尝试谷歌错误信息,你会找到答案。
标签: java email jakarta-mail office365