0

I'm trying to access my email from Java, and it has issues in the connect stage.

import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;

public class CheckingMails {

    public static void main(String args[]) {

        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imaps");
        try {
            Session session = Session.getInstance(props, null);
            Store store = session.getStore();

            System.out.println("aaaaaa");
            store.connect("imap.gmail.com", "myaddress@gmail.com", "mypassword");
            System.out.println("success");

        } catch (NoSuchProviderException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (MessagingException e) {
            e.printStackTrace();
            System.exit(2);
        }
    }
}

Every time I run this code it will print "aaaaaa" telling me that it gets to just before the connection, but then before it can print "success" it takes about a minute and then times out, giving me this error:

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: imap.gmail.com, 993; timeout -1`; 
nested exception is: java.net.ConnectException: Operation timed out

How can I fix this?

dur
  • 15,689
  • 25
  • 79
  • 125
  • 1
    Possible duplicate of [MailConnectException while sending mail using java mail api](http://stackoverflow.com/questions/20766044/mailconnectexception-while-sending-mail-using-java-mail-api) – Tajmin Jun 02 '16 at 13:17
  • Have you checked connectivity with another tool on the same host? eg, telnet imap.gmail.com 993 and see if it connects? – Max Jun 02 '16 at 15:23
  • 1
    Yup the issue was that the wifi I was connected to wouldn't allow me to access imap somehow, I talked to my boss and he got me on a different wifi network and it all works now. Thanks! – Steven DiCarlo Jun 06 '16 at 19:17

0 Answers0