2009-09-28 178 views
0

我试图连接到James服务器本地主机,但我发现了一个异常无法连接到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 

我的詹姆斯服务器的目录结构:

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子目录的配置文件,那么为什么我是 得到那个异常?

下面的代码,它抛出一个异常:

import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Properties; 
import javax.mail.*; 
import javax.mail.internet.*; 

public class mail{ 

public static void main(String[] argts){ 




     String to = "[email protected]"; 



     String from = "[email protected]"; 
     String subject = "Hello"; 
     String body = "What's up"; 

     if ((from != null) && (to != null) && (subject != null) && (body != null)) // we have mail to send 
     { 

     try { 




     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(); 
     } 


     } 

    } 


} 
+0

詹姆斯的控制台输出是什么?尝试'./james start console'。还有:'./james help'。我发现James需要root权限,使用sudo运行。 – Thufir 2012-08-16 23:33:40

回答

1

唯一的例外是说,本地主机不可达。我希望你的机器没有正确配置它的环回网络地址(localhost/127.0.0.1)。

编辑:我假设你在同一台机器上运行客户端和服务器。如果不是,则不能使用localhost/127.0.0.1。

+0

我在同一台机器上运行客户端和服务器;你能告诉我如何配置环回网络地址?我不知道如何为James Server配置它。 – Dusk 2009-09-28 06:18:52

+0

什么操作系统? – 2009-09-28 06:48:47

+0

其实......像这样的问题在superuser.com上问得好 – 2009-09-28 06:49:45

1

我总是尝试telnet到邮件主机上的端口25,以检查是否可以访问服务器。尝试连接到127.0.0.1以检查James是否接受传入连接。我猜你已经检查过詹姆斯的日志了吗?

1

尝试使用IP地址127.0.0.1而不是主机名'localhost' - 可能您的计算机上的DNS查找设置不正确,以至于它不知道名称'localhost'的含义。

打开文件C:\Windows\System32\drivers\etc\hosts,并确保它包含这样一行:

127.0.0.1  localhost 

此外,尝试关闭您的防火墙或防病毒软件。

0
  • 尝试在Linux中以root用户身份启动apache james或在Windows上以 管理员身份启动。并检查服务器是否成功 在日志文件夹启动或不詹姆斯 - server.log文件
  • 添加主机名上您的主机文件

    127.0.0.1  localhost 
    
  • 文件放在

    on linux /etc/hosts 
    on windows C:\Windows\System32\drivers\etc\hosts 
    
  • 和重启你的服务器。