使用Java Mail发送电子邮件

时间:2016-03-21 12:35:19

标签: java email exception

我尝试运行一个小程序,向我的帐户发送通用电子邮件 但我得到一个例外:

  javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 553 We do not relay non-local mail, sorry.

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1873)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1120)
    at javax.mail.Transport.send0(Transport.java:195)
    at javax.mail.Transport.send(Transport.java:124)
    at SendEmail.main(SendEmail.java:54)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 553 We do not relay non-local mail, sorry.

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1724)
    ... 4 more

这是我的代码

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class SendEmail
{
   public static void main(String [] args)
   {    
      // Recipient's email ID needs to be mentioned.
      String to = "Basti-V@web.de";

      // Sender's email ID needs to be mentioned
      String from = "Basti-V@web.de";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

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

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

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

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}

我使用XAMPP控制面板运行Mercury Server。港口是25,79,105,106,110,143和2224.我是新手,所以也许有人可以把我推向正确的方向。

2 个答案:

答案 0 :(得分:1)

您必须取消选中“不允许SMTP中继非本地邮件”选项。

请参阅链接:uncheck option

答案 1 :(得分:0)

请检查以下两件事。 1)。主机邮件服务器正在运行的地方。如果它在本地计算机上运行,​​则将主机地址设置为0.0.0.0

2.如果是外部邮件服务器,请检查用户名,密码,主机等邮件凭据。

我认为553是服务器的“拒绝错误”,这意味着您没有提供正确的凭据。