无法连接到詹姆斯服务器本地主机

发布于 2024-08-06 09:46:11 字数 3391 浏览 6 评论 0原文

我正在尝试连接到 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 服务器的目录结构是:

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

I'm trying to connect to James server localhost, but I'm getting an exception

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

The directory structure of my James Server is:

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

Here's the code, which is throwing an exception:

I've not changed anything in the config file of james-2.3.2 subdirectory, then why I'm
getting that exception?

Here's the code, which is throwing an exception:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫南 2024-08-13 09:46:11

例外情况是 localhost 无法访问。我预计您的计算机没有正确配置其环回网络地址(localhost / 127.0.0.1)。

编辑:我假设您在同一台计算机上运行客户端和服务器。如果没有,则无法使用 localhost / 127.0.0.1。

The exception is saying that localhost is unreachable. I expect that your machine does not have its loopback network address (localhost / 127.0.0.1) correctly configured.

EDIT: I assume that you are running the client and server on the same machine. If not, you cannot use localhost / 127.0.0.1.

一抹苦笑 2024-08-13 09:46:11

我总是尝试远程登录到邮件主机上的端口 25,以检查是否可以访问服务器。尝试连接到 127.0.0.1 以检查 James 是否接受传入连接。我想你已经检查过 James 的日志是否有错误?

I always try to telnet to port 25 on the mailhost, to check if the server can be reached. Try to connect to 127.0.0.1 to check if James is accepting incoming connections. I presume you have checked the logs of James for errors?

岁月无声 2024-08-13 09:46:11

尝试使用 IP 地址 127.0.0.1 而不是主机名“localhost” - 也许您计算机上的 DNS 查找设置不正确,因此它不知道名称“localhost”的含义。

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

127.0.0.1       localhost

另外,请尝试关闭防火墙或防病毒软件。

Try using the IP address 127.0.0.1 instead of the hostname 'localhost' - maybe DNS lookup on your machine is not set up properly, so that it doesn't know what the name 'localhost' means.

Open the file C:\Windows\System32\drivers\etc\hosts, and make sure it contains a line like this:

127.0.0.1       localhost

Also, try switching off your firewall or anti-virus software.

苏佲洛 2024-08-13 09:46:11
  • 尝试在 linux 中以 root 用户身份启动 apache james 或以
    Windows 上的管理员。并检查服务器是否成功
    是否启动在日志文件夹中的 james-server.log 文件上
  • 在主机文件中添加主机名

    <前><代码>127.0.0.1 本地主机

  • 放置文件

    在 Linux /etc/hosts 上
    在 Windows 上 C:\Windows\System32\drivers\etc\hosts
    
  • 并重新启动服务器。
  • try to start apache james as root user in linux or start as
    Administrator on windows. and check the server is successfully
    started or not on james-server.log file in logs folder
  • add the host name on your hosts file

    127.0.0.1       localhost
    
  • file placed

    on linux /etc/hosts
    on windows C:\Windows\System32\drivers\etc\hosts
    
  • and restart your server.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文