如何使用MS Exchange Server发送电子邮件
问题内容:
我正在尝试使用公司的邮件服务器发送电子邮件。但是我收到以下异常
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.1 Client was not authenticated
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
这是我的示例代码,
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", "example.server.com");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", "25");
// Get session
//Session session = Session.getDefaultInstance(props, null);
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username", "password");
}
});
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set the subject
message.setSubject("Hello JavaMail");
// Set the content
message.setText("Welcome to JavaMail");
// Send message
Transport.send(message);
哪段代码是错误的?作为用户名和密码,我正在使用公司的电子邮件地址和密码。
问题答案:
5.7.1可能是由于交换而不是您的代码引起的。您可能只需要在服务器上启用中继。对于匿名用户或从某个IP地址。我不是Exchange方面的专家,但我之前已经做过这项工作。这是我测试过的最后一个有效的解决方案:
如果在通过用户身份验证时尝试在Exchange Server上通过SMTP发送电子邮件时遇到5.7.1错误。
作为参考,您刚刚遇到的问题是由Exchange 2007服务器上的设置引起的–在2003服务器上通常不会出现问题
通过执行以下操作来解决…
您可以通过GUI设置此身份验证设置
- 在服务器配置/集线器传输/默认
中 - 右键单击,属性,权限组
- 选中“匿名用户”,然后单击“确定”。
显然,匿名用户不是太安全,但是您可以看到这是否可以解决问题。