为什么Java MailSender无法识别目标电子邮件?

时间:2014-05-13 10:04:03

标签: java

我有一个有两种方法的MailSender类:

private Session createSmtpSession(final String fromEmail, final String fromEmailPassword) {
    final Properties props = new Properties();
    props.setProperty ("mail.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.auth", "true");
    props.setProperty("mail.smtp.port", "" + 587);
    props.setProperty("mail.smtp.starttls.enable", "true");
    props.setProperty ("mail.transport.protocol", "smtp");


    return Session.getDefaultInstance(props, new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(fromEmail, fromEmailPassword);
      }
    });
}

public void sendEmail(String subject, String fromEmail, String fromEmailPassword, String content,    String toEmail){
    Session mailSession = createSmtpSession(fromEmail, fromEmailPassword);
    mailSession.setDebug (true);

    try {
        Transport transport = mailSession.getTransport ();

        MimeMessage message = new MimeMessage (mailSession);

        message.setSubject (subject);
        message.setFrom (new InternetAddress (fromEmail));
        message.setContent (content, "text/html");
        message.addRecipient (Message.RecipientType.TO, new InternetAddress (toEmail));

        transport.connect ();
        transport.sendMessage (message, message.getRecipients (Message.RecipientType.TO));  
    }
    catch (MessagingException e) {
        System.err.println("Cannot Send email");
        e.printStackTrace();
    }
}

好的,上面代码的工作方式是这样的:

我有个人电子邮件myEmail@gmail.com&如果我想从myEmail@gmail.com发送msg到aa@xyz.com等目标电子邮件,那么我需要拨打sendEmail(subject, "myEmail@gmail.com", fromEmailPassword, content, "aa@xyz.com");

但最近我从Godaddy购买了VPS&我正在使用Godaddy Email(info@mydomain.com),所以我想从info@mydomain.com发送smg到目的地电子邮件,如aa@xyz.com,然后我需要拨打sendEmail(subject, "info@mydomain.com", fromEmailPassword, content, "aa@xyz.com");

但是我使用端口25得到了Godaddy的错误 所以我更改了createSmtpSession中的代码,如下所示:

props.setProperty ("mail.host", "smtp.mydomain.com");
props.setProperty("mail.smtp.port", "" + 25);

然而,这次我遇到了其他问题

DEBUG SMTP: Sending failed because of invalid destination addresses
RSET
250 2.0.0 OK
DEBUG SMTP: MessagingException while sending, THROW: 
javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.1.1 <aa@xyz.com> Recipient not found.  <http://x.co/irbounce>

显然,aa@xyz.com实际存在,但为什么错误说Recipient not found.

SO如何解决此问题?

1 个答案:

答案 0 :(得分:0)

我需要查看Godaddy Email&amp; amp;中的传出(SMTP)服务器(如smtpout.secureserver.net)。现在它工作正常

    props.setProperty ("mail.transport.protocol", "smtps");
    props.setProperty ("mail.smtps.host", "smtpout.secureserver.net");
    props.setProperty("mail.smtps.auth", "true");
    props.setProperty("mail.smtps.port", "" + 465);