JavaMail API和SMTP服务器

时间:2013-12-29 15:49:27

标签: java email javamail

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

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)我应该使用像Apache James这样的第三方SMTP服务器吗?

2 个答案:

答案 0 :(得分:2)

除非PC无法访问互联网,否则您无需安装STMP服务器即可使其正常运行。

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

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

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

答案 1 :(得分:0)

No,您无需安装本地SMTP服务器即可使用Javamail发送电子邮件。

由于these原因

,您收到邮件连接错误

使用this示例使用Gmail via Javamail

发送电子邮件

此外String from = "blah@blah.com";应包含您发送电子邮件的地址

如果您使用本地主机发送电子邮件,就像在这种情况下一样,它应该像String host = "127.0.0.1\"和剩余的配置,但就像我说的那样absolutely unnecessary