Why do i get this exception?

saplingPro picture saplingPro · Jul 23, 2011 · Viewed 12.4k times · Source

This method gives the number of emails in the inbox.But it gives me this exception :

javax.mail.MessagingException: Connect failed;
 nested exception is:
java.net.ConnectException: Connection timed out: connecterror

-

 Session session = Session.getInstance(new Properties());
    try {
        Store store  = session.getStore("pop3");
        store.connect("pop.gmail.com" , "username" , "password");
        Folder fldr = store.getFolder("INBOX");
        fldr.open(Folder.READ_WRITE);
        int count = fldr.getMessageCount();
        System.out.println(count);
    } catch(Exception exc) {
        System.out.println(exc + "error");
    }    

Answer

paulsm4 picture paulsm4 · Jul 23, 2011

Probably because the server refuses to connect.

Try connecting from "telnet". Once you can connect at all, then you should be able to connect from your Java program.

Here are some troubleshooting tips: