Apache Email API:获得例外 - “收件人”地址无效

时间:2011-08-04 07:33:56

标签: java smtp javamail apache-commons

我正在使用Apache commons mail API通过Java程序发送电子邮件。

以下是java代码。

 public static void sendSimpleMail() throws Exception {
    Email email = new SimpleEmail();
    email.setSmtpPort(25);
    email.setDebug(false);
    email.setHostName("localhost");
    email.setFrom("user1@test.com");     // Is this correct ?Do we need this?
    email.setSubject("Hi");
    email.setMsg("This is a test mail ... :-)");
    email.addTo("myname@mycompany.com");
    email.setTLS(true);
    email.send();
    System.out.println("Mail sent!");
}

我使用有效的DNS服务器在端口25上运行Free SMTP server

现在,当我运行程序时,我得到以下异常。

  Exception in thread "main" org.apache.commons.mail.EmailException: Sending the email to the following server failed : localhost:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1242)
at org.apache.commons.mail.Email.send(Email.java:1267)
at TestMail.sendSimpleMail(TestMail.java:26)
at TestMail.main(TestMail.java:13)
Caused by: com.sun.mail.smtp.SMTPSendFailedException: 
  550 Invalid recipient:     myname@mycompany.com

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:1215)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:586)
at javax.mail.Transport.send0(Transport.java:169)
at javax.mail.Transport.send(Transport.java:98)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1232)
... 3 more

现在,我的电子邮件ID是一个非常有效的电子邮件ID。为什么程序会出现这样的错误?

1 个答案:

答案 0 :(得分:0)

550是标准的SMTP错误代码,因此问题似乎位于SMTP服务器配置中的某个位置。验证您是否可以手动向给定的电子邮件地址(telnet localhost 25)发送电子邮件,如果不能,那么它与Apache无关。