Failed to connect to Gmail using IMAP
<p>I have this code to connect to Gmail via IMAP</p>

<pre><code>public static Session getSessionGmail() throws GeneralSecurityException {
    final Properties props = new Properties();
    MailSSLSocketFactory sf = new MailSSLSocketFactory();
    sf.setTrustAllHosts(true);
    props.setProperty("mail.imap.host", "imap.gmail.com");
    props.setProperty("mail.imap.user", gmailUsername);
    props.setProperty("mail.imap.password", password);
    props.setProperty("mail.imap.port", "993");
    props.setProperty("mail.imap.auth", "true");
    props.setProperty("mail.imap.starttls.enable", "true");
    props.put("mail.imap.starttls.enable", "true");
    props.put("mail.imap.ssl.socketFactory", sf);

    Authenticator auth = new Authenticator() {
        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(gmailUsername, password);
        }
    };
    return Session.getDefaultInstance(props, auth);
}
public static Folder openFolder(Session session, String folder) throws MessagingException {
    Store store = session.getStore("imaps");
    store.connect(gmailUsername, password);
    Folder f = store.getFolder(folder);
    f.open(Folder.READ_ONLY);
    return f;
}
</code></pre>

<p>And I always get this exception, pointed at store.connect()</p>

<pre><code>javax.mail.AuthenticationFailedException: failed to connect
at javax.mail.Service.connect(Service.java:332)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:196)
</code></pre>

<p>I have checked all the information from Google, and port for IMAP is 993, if I am right. Username and password are definitely correct. I guess I am missing something but I can't figure it out. </p>

<p>Any help would be much appreciated!</p>

<p><strong>EDIT</strong></p>

<p>After adding mail.debug, I got these logs, but I still stuck at store.connect()</p>

<pre><code>DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
...
DEBUG IMAPS: protocolConnect login, host=imap.gmail.com, user=louis.t@gmail.com, password=&lt;non-null&gt;
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 NO [ALERT] Please log in via your web browser:
</code></pre>

<p>it is logged in, but still stuck at connect ..</p>