Search code examples
javajakarta-mailjames

Fail to connect to james server localhost


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

Solution

  • 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.