客户端未通过身份验证,无法在 MAIL FROM 期间发送匿名邮件
Client was not authenticated to send anonymous mail during MAIL FROM
我正在尝试在调用此方法的 java 应用程序中发送电子邮件:
public static void sendEmail() {
// Create object of Property file
Properties props = new Properties();
// this will set host of server- you can change based on your requirement
props.put("mail.smtp.host", "smtp.office365.com");
// set the port of socket factory
//props.put("mail.smtp.socketFactory.port", "587");
// set socket factory
//props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
// set the authentication to true
props.put("mail.smtp.auth", "true");
// set the port of SMTP server
props.put("mail.smtp.port", "587");
// This will handle the complete authentication
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xx@mail.com", "xx");
}
});
try {
// Create object of MimeMessage class
Message message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress("xx@mail.com"));
// Set the recipient address
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("yy@mail.com"));
// Add the subject link
message.setSubject("Testing Subject");
// Create object to add multimedia type content
BodyPart messageBodyPart1 = new MimeBodyPart();
// Set the body of email
messageBodyPart1.setText("This is message body");
// Create object of MimeMultipart class
Multipart multipart = new MimeMultipart();
// add body part 2
multipart.addBodyPart(messageBodyPart1);
// set the content
message.setContent(multipart);
// finally send the email
Transport.send(message);
System.out.println("=====Email Sent=====");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
但由于某种原因,当调试命中 Transport.send() 时,我得到了这个异常:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [DB6PR0802CA0037.eurprd08.prod.outlook.com]
为什么即使我使用了 Authenticator() 也会发生这种情况?
如果您通过端口 587 连接,您最初会启动一个普通连接,您必须通过显式发送 STARTTLS-command 来启动 TLS。您必须告诉 JavaMail 这样做,否则它将尝试不安全地进行。除非建立 TLS-connection,否则 SMTP-server 不会发送任何 authentication-mechanism-informations,因此 JavaMail 假设不需要身份验证并尝试在没有身份验证的情况下发送邮件。
将以下条目添加到属性中:
props.put("mail.smtp.starttls.enable", "true");
JavaMail 应该在尝试验证之前尝试切换到 TLS。
如果失败,您需要通过设置 属性
来启用调试
props.put("mail.debug", "true");
和post这里的输出。
我正在尝试在调用此方法的 java 应用程序中发送电子邮件:
public static void sendEmail() {
// Create object of Property file
Properties props = new Properties();
// this will set host of server- you can change based on your requirement
props.put("mail.smtp.host", "smtp.office365.com");
// set the port of socket factory
//props.put("mail.smtp.socketFactory.port", "587");
// set socket factory
//props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
// set the authentication to true
props.put("mail.smtp.auth", "true");
// set the port of SMTP server
props.put("mail.smtp.port", "587");
// This will handle the complete authentication
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xx@mail.com", "xx");
}
});
try {
// Create object of MimeMessage class
Message message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress("xx@mail.com"));
// Set the recipient address
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("yy@mail.com"));
// Add the subject link
message.setSubject("Testing Subject");
// Create object to add multimedia type content
BodyPart messageBodyPart1 = new MimeBodyPart();
// Set the body of email
messageBodyPart1.setText("This is message body");
// Create object of MimeMultipart class
Multipart multipart = new MimeMultipart();
// add body part 2
multipart.addBodyPart(messageBodyPart1);
// set the content
message.setContent(multipart);
// finally send the email
Transport.send(message);
System.out.println("=====Email Sent=====");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
但由于某种原因,当调试命中 Transport.send() 时,我得到了这个异常:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [DB6PR0802CA0037.eurprd08.prod.outlook.com]
为什么即使我使用了 Authenticator() 也会发生这种情况?
如果您通过端口 587 连接,您最初会启动一个普通连接,您必须通过显式发送 STARTTLS-command 来启动 TLS。您必须告诉 JavaMail 这样做,否则它将尝试不安全地进行。除非建立 TLS-connection,否则 SMTP-server 不会发送任何 authentication-mechanism-informations,因此 JavaMail 假设不需要身份验证并尝试在没有身份验证的情况下发送邮件。
将以下条目添加到属性中:
props.put("mail.smtp.starttls.enable", "true");
JavaMail 应该在尝试验证之前尝试切换到 TLS。
如果失败,您需要通过设置 属性
来启用调试props.put("mail.debug", "true");
和post这里的输出。