JavaMail API 和 SMTP 服务器

标签 java email jakarta-mail

我正在尝试使用 javamail api 发送电子邮件,看起来我必须安装 SMTP 服务器才能使其正常工作。我在 Windows 8 上的 IIS 6 上启用了默认 SMTP 服务器。我按照以下博客用于设置 http://pdhewaju.com.np/blog/how-to-install-smtp-on-windows-8-developer-preview/ 。但当我运行 JavaMail API 的 wiki 页面上给出的代码示例时,我仍然遇到以下异常:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:295)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at EmailTester.main(EmailTester.java:47)
    Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:321)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:237)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 7 more

代码如下:

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

public class EmailTester
{
   public static void main(String [] args)
   {    
      // Recipient's email ID needs to be mentioned.
      String to = "mymail@gmail.com";//My email address

      // Sender's email ID needs to be mentioned
      String from = "blah@blah.com";// Should this be a real email address?

      // 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);
      properties.put("mail.debug", "true");
      // Get the default Session object.
      Session session = Session.getInstance(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();
      }
   }
}

所以我有多个问题: 1) 在 IIS 上安装 SMTP 服务器与 JavaMail API 一起使用是个好主意吗?我做的事情完全错了吗?

2) 我应该使用第三方 SMTP 服务器(例如 Apache James)吗?

最佳答案

不,您不需要安装 STMP 服务器即可使其正常工作,除非 PC 无法访问互联网。

String host = "localhost"; 行上,您需要将 localhost 更改为 Internet 上 SMTP 服务器的地址。

查看您的电子邮件客户端设置并使用您在那里的地址进行开发。

但是,当您的应用程序上线时,您可能需要更改它,因为如果您的生产环境与实时环境位于不同的网络上,那么它们可能有不同的 SMTP 服务器。而且 SMTP 服务器通常不喜欢处理来自外部网络的电子邮件。这称为“中继”,他们禁止这样做以控制垃圾邮件。

关于JavaMail API 和 SMTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20826948/

相关文章:

java - 无需身份验证/没有发件人密码即可发送 java 电子邮件

java - 尝试在 tomcat 6.0.14 服务器中发送邮件时抛出异常

java - 使用 Eclipse JavaSE 1.6 安装 JavaMail

javafx vbox 和 gridpane

java - 使用 MongoDB 3.2.0 Java 驱动程序提取多个字段

java - 无法使用 log4j SMTP Appender 发送电子邮件。获取 "SMTPSendFailedException: 530 5.7.1 Authentication required"

html - 是否可以使用 HTML 通过电子邮件发送 vim 的内容

vba - 通过从带有换行符 : VBA 的单元格值创建消息来在电子邮件中新建行

java - 递归查找矩阵中的路径

java - 如何修复 "Constructor Calls Overridable Method"