【问题标题】:Cannot send email using Java code on Windows Server 2008 r2?无法在 Windows Server 2008 r2 上使用 Java 代码发送电子邮件?
【发布时间】:2013-12-09 09:06:04
【问题描述】:

我已经在 windows 2008 r2 服务器上安装了 SMTP 服务器和 IIS Web 服务器。我正在尝试通过 localhost 使用 java 代码发送测试电子邮件,但我无法发送电子邮件我收到以下错误,不确定我做错了什么。除了安装 SMTP 服务器,还有什么我需要做的设置,因为我刚刚安装了我的 smtp 服务器并期望这个代码可以工作?

javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for marshell@gmail.com

at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1862)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1118)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at LotusNotes.SendEmail.main(SendEmail.java:30)
Caused by: com.sun.mail.smtp.SMTPAddressFailedException: 550 5.7.1 Unable to relay for marshell@gmail.com at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1715)

Java 代码

public static void main(String[] args) {


  String to = "marshell@gmail.com";
      String from = "imrmsmtpmail";
      String host = "localhost";

      Properties properties = System.getProperties();
      properties.setProperty("mail.smtp.host", host);

      Session session = Session.getDefaultInstance(properties);

      try{

         MimeMessage message = new MimeMessage(session);
         message.setFrom(new InternetAddress(from));
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
         message.setSubject("Subject Line!");
         message.setText("Test email!");
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }
      catch (MessagingException mex) {
         mex.printStackTrace();
      }

}

【问题讨论】:

标签: java windows-server-2008-r2


【解决方案1】:

您可能必须进行身份验证才能将电子邮件发送到此服务器。

AFAIK,您没有在与服务器的连接中提供任何用户或密码。

我使用这样的东西:

  Properties properties = System.getProperties();
  properties.setProperty("mail.smtp.host", host);
  properties.setProperty("mail.smtp.auth", true);
  properties.setProperty("mail.user", "PUT AN USERNAME HERE");
  properties.setProperty("mail.password", "PUT A PASSWORD HERE");
  Session session = Session.getDefaultInstance(properties);

为了确保在编写程序之前所有参数都正常工作,对邮件端口 (25) 进行 telnet 很有用,以确保您可以发送电子邮件,直接将代码写入服务器。

在下面的链接中你有一个例子:

http://support.microsoft.com/kb/153119/en

虽然听起来技术性很强,但值得尝试确保服务器使用给定参数从您的计算机发送电子邮件:用户名(或根本不是用户名)、目标地址等。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多