JavaMail SMTPSendFailedException [英] JavaMail SMTPSendFailedException

查看:370
本文介绍了JavaMail SMTPSendFailedException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JavaMail API编写批量电子邮件程序.我有一台Microsoft Exhange服务器,我正尝试将电子邮件发送到该服务器.当我运行程序时,出现以下错误:

I am writing a bulk email program using the JavaMail api. I have a Microsoft Exhange server which I am trying to send the emails in to. When I run my program I get the following error:

**com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1862)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1100)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at SendEmail.postMail(SendEmail.java:100)
at EmailGenerator.main(EmailGenerator.java:52)**

我的代码尝试发送消息的部分如下:

The part of my code trying to send the message is as follows:

Properties props = new Properties();
props.put("mail.smtp.host", email_server);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", true);

class EmailAuthenticator extends Authenticator {
String user;
String pw;
EmailAuthenticator (String FROM, String PASSWORD)
{
    super();
    this.user = FROM;
    this.pw = PASSWORD;
}
public PasswordAuthentication getPasswordAuthentication()
{
    return new PasswordAuthentication(user, pw);
}
}

Session session = Session.getInstance(props, new EmailAuthenticator(USER, PASSWORD));
session.setDebug(debug);
System.out.println("Session created");

.. CREATED MESSAGE HERE...

Transport transport = session.getTransport("smtp");
transport.connect(exchange_server,user,password);
transport.send(msg);
transport.close();

我想知道我是否在Exchange服务器端缺少某些配置,还是我的代码有问题?

I wonder am I missing some configuration on the Exchange server side, or is an issue with my code?

推荐答案

好,我弄清楚了我在哪里出错了,并发布了答案,以防其他人可以从中获得一些价值.我有以下代码行:

OK I figured out where I was going wrong here and am posting up the answer incase anybody else can get some value out of it. I had the following line of code:

props.put("mail.smtp.auth", true);

这是告诉我的应用程序它需要向SMTP服务器进行身份验证,而实际上并不需要.这导致我的应用程序无法登录SMTP服务器并发送电子邮件,从而产生错误消息.将此属性设置为false或没有此代码行对我来说解决了这个问题.这行代码仅对于需要您登录的SMTP服务器是必需的,而我的Exchange服务器则不需要.

This was telling my application that it needed to authenticate to the SMTP server, when in fact it didnt. This was causing my application from logging into the SMTP server and sending the email and thus producing the error message. Setting this property to false or not having this line of code fixed the issue for me. This line of code is only necessary for SMTP servers that require you to login, which my Exchange server didnt.

这篇关于JavaMail SMTPSendFailedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆