JavaMail - 客户端未通过身份验证,无法在 MAIL FROM 期间发送匿名邮件

JavaMail - Client was not authenticated to send anonymous mail during MAIL FROM

我正在尝试使用 JavaMail 通过 office365 有效帐户发送电子邮件。但是,我收到相同的异常 '530 5.7.57 SMTP;在 MAIL FROM 期间,客户端未通过身份验证以发送匿名邮件。按照我用于测试的简单代码:

Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.office365.com");
props.put("mail.smtp.port", "587");
props.put("mail.debug", "true");

Session session = Session.getInstance(props, new Authenticator() {
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(USER_NAME, USER_PASS);
    }
});

try {
    final Message message = new MimeMessage(session);
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(USER_NAME));
    message.setFrom(new InternetAddress(USER_NAME));
    message.setSubject(subject);
    message.setText(messageContent);
    message.setSentDate(new Date());
    Transport.send(message);
    System.out.println("Send OK");
} catch (final MessagingException ex) {
    System.out.println(ex.getMessage());
}

调试输出如下:

DEBUG: JavaMail version 1.3
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.8.0_171\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.8.0_171\lib\javamail.address.map (The system cannot find the file specified)
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true

DEBUG: SMTPTransport trying to connect to host "smtp.office365.com", port 587

DEBUG SMTP RCVD: 220 xxxxx.outlook.office365.com Microsoft ESMTP MAIL Service ready at Thu, 6 Jun 2019 07:53:32 +0000

DEBUG: SMTPTransport connected to host "smtp.office365.com", port: 587

DEBUG SMTP SENT: EHLO L3343005201
DEBUG SMTP RCVD: 250-xxxxx.outlook.office365.com Hello [92.242.173.14]
250-SIZE 157286400
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 SMTPUTF8

DEBUG SMTP Found extension "SIZE", arg "157286400"
DEBUG SMTP Found extension "PIPELINING", arg ""
DEBUG SMTP Found extension "DSN", arg ""
DEBUG SMTP Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP Found extension "STARTTLS", arg ""
DEBUG SMTP Found extension "8BITMIME", arg ""
DEBUG SMTP Found extension "BINARYMIME", arg ""
DEBUG SMTP Found extension "CHUNKING", arg ""
DEBUG SMTP Found extension "SMTPUTF8", arg ""
DEBUG SMTP: use8bit false
DEBUG SMTP SENT: MAIL FROM:<sender@domain>
DEBUG SMTP RCVD: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [xxxxx.yyyyy.outlook.com]

DEBUG SMTP SENT: QUIT
Sending failed;
  nested exception is:
    class javax.mail.MessagingException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [xxxxx.yyyyy]

从日志来看,身份验证似乎不起作用,连接端口变为 25 而不是属性中指定的 587。

我也尝试添加其他属性,比如

props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.user", USER_NAME);
props.put("mail.smtp.password", USER_PASS);
props.put("mail.smtp.from", USER_NAME);

任意组合,但没有结果。 我也试过使用更新版本的 JavaMail 库,但异常是一样的。我是否缺少邮件帐户的任何配置?还是来自代码?

提前致谢

已编辑:将端口从 587(整数)更改为“587”(字符串)解决了调试端口中的端口问题,但出现相同的客户端身份验证错误

JavaMail 1.3 已经 17 岁了!你在哪里找到它?切换到 current release and then fix all these common JavaMail mistakes.