【问题标题】:why does JavaMail to Exchange throw MessagingException 530 5.7.1 Client was not authenticated?为什么 JavaMail to Exchange 会抛出 MessagingException 530 5.7.1 Client was not authenticated?
【发布时间】:2013-11-27 19:27:32
【问题描述】:

我已经对此进行了一段时间的研究,并尝试了几种不同的属性组合并不断收到此错误。我们能够毫无问题地使用其他 smtp 服务器(和不同的属性)成功发送电子邮件,但 Exchange 无法正常工作。

关于此主题的其他答案提到您需要 1.4.3 版或更新版本的 JavaMail,因为 Exchange 服务器需要 NTLM。我正在使用 JavaMail 1.4.7。其他答案说 Exchange 默认不支持 SMTP。

这是最新的代码:

// this is inside a Junit
String emailTo = "me@gmail.com";
String emailFrom = "me@mycompany.com";
String message = "testing exchange";
String host = "smtp.office365.com";
String user = "obfuscated";
String password = "obfuscated";

StringBuilder body = new StringBuilder(message);

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

Authenticator authenticator = new Authenticator();
props.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());

Session session = Session.getInstance(props, authenticator);

Message message = new MimeMessage(session);
try {
    message.setFrom(new InternetAddress(as_email_from));
    message.setRecipient(Message.RecipientType.TO, new InternetAddress(as_email_to));
    message.setSubject("Testing exchange server");                
    message.setContent(body.toString(), "text/html");
    message.setSentDate(new Date());

    Transport transport = session.getTransport("smtp");
    transport.connect(host, 587, smtpSettings.getUser(), smtpSettings.getPswd());
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

} catch (Exception e) {
    e.printStackTrace();
}

private class Authenticator extends javax.mail.Authenticator {
   private PasswordAuthentication authentication;
   public Authenticator() {
       String username = "myuser";
       String password = "mypass";
       authentication = new PasswordAuthentication(username, password);
   }
   protected PasswordAuthentication getPasswordAuthentication() {
       return authentication;
   }
}

这是调试:

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

DEBUG SMTP SENT: EHLO Brian-PC
DEBUG SMTP RCVD: 250-BY2PR01CA020.outlook.office365.com Hello [207.250.96.62]
250-SIZE 78643200
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-AUTH
250-8BITMIME
250-BINARYMIME
250 CHUNKING

DEBUG SMTP Found extension "SIZE", arg "78643200"
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 "AUTH", arg ""
DEBUG SMTP Found extension "8BITMIME", arg ""
DEBUG SMTP Found extension "BINARYMIME", arg ""
DEBUG SMTP Found extension "CHUNKING", arg ""
DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: use8bit false
DEBUG SMTP SENT: MAIL FROM:<me@mycompany.com>
DEBUG SMTP RCVD: 530 5.7.1 Client was not authenticated

【问题讨论】:

    标签: exchange-server jakarta-mail


    【解决方案1】:

    根据调试输出,我相信您实际上并没有使用 JavaMail 1.4.7。检查您的 CLASSPATH、服务器上的 lib 目录等。

    【讨论】:

    • 调试输出告诉你什么?
    • 协议跟踪的格式。格式在最近的版本中发生了变化。此外,最近的版本会在开头打印版本号。
    • 谢谢,这确实是问题所在。我现在可以通过 Exchange 服务器发送电子邮件。而且我确实看到在调试开始时打印了 1.4.7 版本。
    • 太棒了!您可能需要考虑升级到 JavaMail 1.5.1。
    【解决方案2】:

    检查您的 Exchange 服务器是否需要启用 TLS,如果需要,请通过设置此属性来启用 TLS

    mail.smtp.starttls.enable=true
    

    这解决了我的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-18
      • 2021-12-17
      • 1970-01-01
      • 2020-06-15
      相关资源
      最近更新 更多