我正在尝试连接到James服务器本地主机,但出现异常
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port:25;
nested exception is:
java.net.SocketException: Network is unreachable: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1545)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at mail.main(mail.java:78)
Caused by: java.net.SocketException: Network is unreachable: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(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:267)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:227)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1511)
... 7 more
我的James Server的目录结构是:
C:\apache-james-2.3.2
|
|
|
|------C:\apache-james-2.3.2\james-2.3.2
|
|------C:\apache-james-2.3.2\javamail-1.4.2
|
|------C:\apache-james-2.3.2\jaf-1.0.2
这是代码,它引发异常:
我没有更改james-2.3.2子目录的配置文件中的任何内容,所以我为什么得到那个例外?
这是代码,它引发异常:
// imports omitted
public class mail {
public static void main(String[] args) {
String to = "blue@localhost";
String from = "red@localhost";
String subject = "Hello";
String body = "What's up";
if ((from != null) && (to != null) && (subject != null) && (body != null)) {
try { // we have mail to send
Properties props = new Properties();
props.put("mail.host", "localhost");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("red", "red");
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
Address[] add = { new InternetAddress(to) };
message.setRecipients(Message.RecipientType.TO, add);
message.setSubject(subject);
message.setContent(body, "text/plain");
message.setText(body);
Transport.send(message);
System.out.println(" Your message to " + to + " was successfully sent.");
} catch (Throwable t) {
t.printStackTrace();
}
}
}
}
例外是说本地主机不可访问。我希望您的计算机没有正确配置其回送网络地址(localhost / 127.0.0.1)。
编辑:我假设您正在同一台计算机上运行客户端和服务器。如果不是,则不能使用localhost / 127.0.0.1。
我总是尝试远程登录到邮件主机上的端口25,以检查是否可以访问服务器。尝试连接到127.0.0.1,以检查James是否接受传入的连接。我想您已经检查了詹姆斯的日志中是否有错误?
尝试使用IP地址127.0.0.1代替主机名'localhost'-可能在您的计算机上未正确设置DNS查找,因此它不知道名称'localhost'意味着什么。
打开文件C:\Windows\System32\drivers\etc\hosts
,并确保它包含这样的行:
127.0.0.1 localhost
也请尝试关闭防火墙或防病毒软件。
在主机文件中添加主机名
127.0.0.1 localhost
已放置文件
on linux /etc/hosts
on windows C:\Windows\System32\drivers\etc\hosts