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

标签 java jakarta-mail 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

我的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 子目录的配置文件中的任何内容,那为什么我 得到那个异常(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();
            }
        }
    }
}

最佳答案

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

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

关于java - 无法连接到詹姆斯服务器本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1485583/

相关文章:

java - ImageJ API : Getting RGB values from image

java - Spring - 从 bean 数据源获取 driverClassName

Java EE api邮件发送邮件时出错

java - 通过javamail连接错误

java - Apache James 2.3.2 不会写入 MySQL

gmail - 在 James Mail Server 中发送外部邮件的问题

java - 无法在 netbeans 上找到文件

java - Java 14+ Arrays.sort( int[] ) 的最坏情况时间复杂度是多少?

Javamail imap getMessages 返回 null 数组

java - 如何使用 javax mail 归档电子邮件