简体   繁体   中英

Failed to connect to Gmail using IMAP

I have this code to connect to Gmail via IMAP

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;
}

And I always get this exception, pointed at store.connect()

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)

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.

Any help would be much appreciated!

EDIT

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

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=<non-null>
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A1 NO [ALERT] Please log in via your web browser:

it is logged in, but still stuck at connect ..

Try it this way (note the imap s in the strings)

    sf.setTrustAllHosts(true);
    props.setProperty("mail.imaps.host", "imap.gmail.com");
    props.setProperty("mail.imaps.user", gmailUsername);
    props.setProperty("mail.imaps.password", password);
    props.setProperty("mail.imaps.port", "993");
    props.setProperty("mail.imaps.auth", "true");
    props.setProperty("mail.debug", "true");

Ran into error for "connecting from insecure decives" for my - but there's a known way around that in gmail settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM