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

时间:2019-06-06 08:18:15

标签: java smtp office365 javamail

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

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”(字符串)解决了调试端口中的端口问题,但发生了相同的客户端身份验证错误

1 个答案:

答案 0 :(得分:0)

JavaMail 1.3已有17年的历史了!你在哪里找到它?切换到current release,然后修复所有这些common JavaMail mistakes