views:

459

answers:

3

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:

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 = "blue@localhost";



        String from = "red@localhost";
        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();
        }


        }

    }


}
+1  A: 

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.

Stephen C
I'm running the client and server on the same machine; could you tell me how can I configure loopback network address? I've no idea about how to configure it for James Server.
Dusk
What operating system?
Stephen C
Actually ... questions like that are better asked on superuser.com
Stephen C
+1  A: 

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?

Jeroen van Bergen
+1  A: 

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.

Jesper