Search code examples
javanetwork-programmingjakarta-mail

MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 110; timeout -1;


I'm trying to read messages from my inbox, but I'm keep getting exception "MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 110; timeout -1;"

I disabled my AntiVir and Firewall, but doesn't help.

Below is code and console report:

public class JavaMailPOP3eMail {

    private String server = null;
    private String user = null;
    private String pass = null;

    public void sendMail() {
        server = "smtp.gmail.com";
        user = "[email protected]";
        pass = "pass123";

        Store store = null;
        Folder folder = null;
        try {
            // get default session
            Properties properties = System.getProperties();
            Session session = Session.getDefaultInstance(properties, null);
            session.setDebug(true);
            // get a pop3 message store, and connect to it
            store = session.getStore("pop3");
            store.connect(server, user, pass);
            // get the default folder
            folder = store.getDefaultFolder();
            if (folder == null) {
                throw new Exception("No default folder");
            }
            // get the inbox
            folder = folder.getFolder("INBOX");
            if (folder == null) {
                throw  new Exception("No POP3 INBOX");
            }
            // open the folder read only
            folder.open(Folder.READ_ONLY);
            // get the messages and print them
            Message[] messages = folder.getMessages();
            for (int i = 0; i < messages.length; i++) {
                printMail(messages[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (folder != null) {
                    folder.close(false);
                }
                if (store != null) {
                    store.close();
                }
            } catch (MessagingException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void printMail(Message message) {
        try {
            // get header information
            String from = null;
            from = ((InternetAddress) message.getFrom()[0]).getPersonal();
            // print sender details
            System.out.println("From: " + from);
            // get and print subject
            String subj = message.getSubject();
            System.out.println("Subject: " + subj);
            // get the message itself
            Part messagePart = message;
            Object content = messagePart.getContent();
            if (content instanceof Multipart) {
                messagePart = ((Multipart) content).getBodyPart(0);
                System.out.println("[ Multipart Message ]");
            }
            // get the content type
            String contentType = messagePart.getContentType();
            // if the content is plain text, print it
            System.out.println("Content: " + contentType);
            if (contentType.startsWith("text/plain") || 
                contentType.startsWith("text/html")) {
                InputStream is = messagePart.getInputStream();
                BufferedReader br = new BufferedReader(
                    new InputStreamReader(is)
                );
                String line = br.readLine();
                while (line != null ) {
                    System.out.println(line);
                    line = br.readLine();
                }
            }
            System.out.println("");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JavaMailPOP3eMail mail = new JavaMailPOP3eMail();
        mail.sendMail();
    }

}

Exception:

DEBUG: setDebug: JavaMail version 1.5.2
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]
DEBUG POP3: mail.pop3.rsetbeforequit: false
DEBUG POP3: mail.pop3.disabletop: false
DEBUG POP3: mail.pop3.forgettopheaders: false
DEBUG POP3: mail.pop3.cachewriteto: false
DEBUG POP3: mail.pop3.filecache.enable: false
DEBUG POP3: mail.pop3.keepmessagecontent: false
DEBUG POP3: mail.pop3.starttls.enable: false
DEBUG POP3: mail.pop3.starttls.required: false
DEBUG POP3: mail.pop3.apop.enable: false
DEBUG POP3: mail.pop3.disablecapa: false
DEBUG POP3: connecting to host "smtp.gmail.com", port 110, isSSL false
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 110; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:211)
    at javax.mail.Service.connect(Service.java:364)
    at javax.mail.Service.connect(Service.java:245)
    at example3.JavaMailPOP3eMail.sendMail(JavaMailPOP3eMail.java:43)
    at example3.JavaMailPOP3eMail.main(JavaMailPOP3eMail.java:118)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:312)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
    at com.sun.mail.pop3.Protocol.<init>(Protocol.java:112)
    at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:264)
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:207)

EDIT:

POP is enabled but still doesn't work

enter image description here

Do you have any other idea what the reason could be ?

DEBUG: setDebug: JavaMail version 1.5.2
DEBUG: getProvider() returning javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]
DEBUG POP3: mail.pop3.rsetbeforequit: false
DEBUG POP3: mail.pop3.disabletop: false
DEBUG POP3: mail.pop3.forgettopheaders: false
DEBUG POP3: mail.pop3.cachewriteto: false
DEBUG POP3: mail.pop3.filecache.enable: false
DEBUG POP3: mail.pop3.keepmessagecontent: false
DEBUG POP3: mail.pop3.starttls.enable: false
DEBUG POP3: mail.pop3.starttls.required: false
DEBUG POP3: mail.pop3.apop.enable: false
DEBUG POP3: mail.pop3.disablecapa: false
DEBUG POP3: connecting to host "pop.gmail.com", port 110, isSSL false
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: pop.gmail.com, 110; timeout -1;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:211)  at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:211)
    at javax.mail.Service.connect(Service.java:364)
    at javax.mail.Service.connect(Service.java:245)
    at example3.JavaMailPOP3eMail.sendMail(JavaMailPOP3eMail.java:41)
    at example3.JavaMailPOP3eMail.main(JavaMailPOP3eMail.java:116)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:312)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
    at com.sun.mail.pop3.Protocol.<init>(Protocol.java:112)
    at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:264)
    at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:207)
    ... 4 more

Solution

  • According to this link from Gmail, the server should be pop.gmail.com (instead of smtp.gmail.com) and you have to allow POP access to your account for this to work.