MS Exchange Server NTLM身份验证问题501 5.1.7地址无效

时间:2019-01-24 05:14:29

标签: java email javamail exchange-server

我有一个Web应用程序,需要将电子邮件通知发送给用户以执行特定任务。

我有一个屏幕,该站点的管理员在其中配置邮件服务器设置,然后应用程序将在需要时使用该设置发送电子邮件。

下面是我们拥有的代码的高度精简版本:

public static boolean sendEmail(String toAddress, String fromAddress, String userName, String userPassword,String smtpHost, String portNumber, String emailSubject,String emailBody) {
      // Recipient's email ID needs to be mentioned.

       String to = toAddress;

      // Sender's email ID needs to be mentioned
      String from = fromAddress;//change accordingly
      final String username = userName;//change accordingly
      final String password = userPassword;//change accordingly

      // Assuming you are sending email through relay.jangosmtp.net
      String host = smtpHost;

      Properties props = new Properties();

      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.socketFactory.fallback", "false");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.socketFactory.port", portNumber);
      props.put("mail.smtp.host", host);
      props.put("mail.smtp.port", portNumber);

      //For NTLM
      props.setProperty("mail.imap.auth.ntlm.domain","");

      // Get the Session object.
      SMTPAuthenticator authenticator = new SMTPAuthenticator(username, password);
      props.put("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName());
      Session session = Session.getInstance(props, authenticator);


      try {
         // Create a default MimeMessage object.
         Message message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.setRecipients(Message.RecipientType.TO,
         InternetAddress.parse(to));

         // Set Subject: header field
         message.setSubject(emailSubject);

         // Now set the actual message
         message.setText(emailBody);

         // Send message
         Transport.send(message);

         System.out.println("Sent message successfully....");

      } catch (MessagingException e) {
            throw new RuntimeException(e);
      }
    return true;
   }

它运行良好,直到我们在一个客户端环境中部署了应用程序。我们的客户使用的本地MS Exchange Server使用 NTLM身份验证

在这里,我们需要将发件人和发件人地址指定为MYDOMAIN\s.sriram,而不是s.sriram@mydomain.com

上面的代码在这种情况下不起作用,这会引发以下异常:

Exception in thread "main" java.lang.RuntimeException: com.sun.mail.smtp.SMTPSendFailedException: 501 5.1.7 Invalid address
;
  nested exception is:
com.sun.mail.smtp.SMTPSenderFailedException: 501 5.1.7 Invalid address

at com.ycs.tenjin.mail.EmailUtil.sendEmail(EmailUtil.java:86)
at com.ycs.tenjin.mail.EmailUtil.main(EmailUtil.java:19)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 501 5.1.7 Invalid address
;
  nested exception is:
com.sun.mail.smtp.SMTPSenderFailedException: 501 5.1.7 Invalid address

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at com.ycs.tenjin.mail.EmailUtil.sendEmail(EmailUtil.java:80)
... 1 more
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 501 5.1.7 Invalid address

at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1616)
... 5 more

但是,如果我尝试将from和to地址设置为常规格式,即s.sriram@mydomain.com,则会收到身份验证失败错误,如下所示:

Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful

at com.ycs.tenjin.mail.EmailUtil.sendEmail(EmailUtil.java:85)
at com.ycs.tenjin.mail.EmailUtil.main(EmailUtil.java:19)
Caused by: javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.ycs.tenjin.mail.EmailUtil.sendEmail(EmailUtil.java:79)
... 1 more

无论哪种方式,这都不起作用。

现在,该客户告诉我,他有另一个应用程序,可以在该应用程序中成功配置和使用同一MS Exchange服务器发送电子邮件,并且指定的发件人和发件人地址为MYDOMAIN\s.sriram。甚至还向我显示了另一个应用程序的电子邮件配置屏幕,其中有一个选项可以明确选择 NTLM 作为身份验证类型。所有其他参数与我上面的代码段中描述的参数相同。

是否可以将身份验证类型设置为NTLM而不是SMTP?

如果没有,有人可以提出解决这个问题的方法吗?

0 个答案:

没有答案