我正在尝试使用 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/