【发布时间】:2012-12-14 21:37:06
【问题描述】:
我使用以下 Java 代码发送电子邮件。
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendEmail
{
public static void main(String [] args)
{
String to = "abcd@gmail.com";
String from = "web@gmail.com";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("smtp.gmail.com", 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("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
当我运行该文件时,我收到以下错误:
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:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
如果有人可以帮助我,我将不胜感激。
ConnectException如何解决?
【问题讨论】:
-
您可以尝试在配置的 SMTP 端口(默认值:25)上远程登录到您的本地主机。
telnet localhost 25并检查? -
那么,您的机器上运行着 SMTP 服务器吗?
-
我试过 telnet localhost 25 bt 它错误显示 Connecting To localhost...Could not open connection to the host, on port 25: Connect failed 我在 Windows 功能中启用了 telnet 客户端和 telnet 服务器功能,我使用的是 Windows 7
-
这里可能有更多关于调试 telnet 超时的信息:stackoverflow.com/questions/5179807/…
标签: java email jakarta-mail connectexception