Skip to content
Advertisement

javax.mail.MessagingException: Connect timed out; [closed]

I need to fetch the Zoho mail using Java. I enabled imaps in Zoho mail settings, but I am getting “javax.mail.MessagingException: Connect timed out” error in the following code.

public class Mail {

    public static void main(String[] args) {
        try {
            final Properties props = new Properties();
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        props.setProperty("mail.store.protocol", "imaps");
        props.setProperty("mail.imaps.port", "993");
        props.setProperty("mail.imaps.connectiontimeout", "5000");
        props.setProperty("mail.imaps.timeout", "5000");
        props.setProperty("mail.imaps.auth", "true");
        props.setProperty("mail.debug", "true");
        
            Session session = Session.getInstance(props, null);
            session.setDebug(true);
            Store store = session.getStore();
            store.connect("imap.zoho.in", "*@zohomail.in", "****");
            Folder inbox = store.getFolder("INBOX");
            inbox.open(Folder.READ_ONLY);
            Message msg = inbox.getMessage(inbox.getMessageCount());
            Address[] in = msg.getFrom();
            for (Address address : in) {
                System.out.println("FROM:" + address.toString());
            }
            Multipart mp = (Multipart) msg.getContent();
            BodyPart bp = mp.getBodyPart(0);
            System.out.println("SENT DATE:" + msg.getSentDate());
            System.out.println("SUBJECT:" + msg.getSubject());
            System.out.println("CONTENT:" + bp.getContent());
        } catch (Exception mex) {
            mex.printStackTrace();
        }
    }
}

The error:

DEBUG: trying to connect to host "imap.zoho.in", port 993, isSSL true
javax.mail.MessagingException: Connect timed out;
     nested exception is:
    java.net.SocketTimeoutException: Connect timed out
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:611)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at Mail.main(Mail.java:86)
    Caused by: java.net.SocketTimeoutException: Connect timed out
    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:546)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at java.base/sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:304)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:265)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:107)
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:103)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:578)
    ... 3 more

Advertisement

Answer

My system has been connected to the office wifi network.So the firewall didn’t allow me to connect to the port.When I switch my wifi network ,the program runs perfectly !

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement