无法使用IMAP连接到Gmail

时间:2016-01-05 15:38:57

标签: java gmail javamail imap gmail-imap

我有此代码通过IMAP连接到Gmail

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

我总是得到这个例外,指向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)

我已经检查了Google的所有信息,如果我是对的,IMAP的端口是993。用户名和密码绝对正确。我想我错过了一些东西,但我无法理解。

非常感谢任何帮助!

修改

添加mail.debug后,我收到了这些日志,但我仍然停留在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:

它已登录,但仍然停留在连接..

1 个答案:

答案 0 :(得分:0)

以这种方式试试(注意字符串中的imap s

    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");

因为不安全的决定与#34;连接错误。对于我 - 但在gmail设置中已经知道了这一点。