Android JavaMail IMAP商店未通过OAuth2.0身份验证连接到Gmail

时间:2015-08-25 19:02:05

标签: android oauth-2.0 gmail javamail imap

我有Android应用程序,使用Gmail检查和发送电子邮件。 由于需要为不太安全的应用程序打开/关闭访问权限,我将其移至OAuth2.0身份验证,因为它不适合我。

它验证正常,我得到令牌和ABLE使用SMTPTransport发送电子邮件。

但是,使用IMAP检查/接收电子邮件不起作用!它到达store.connect然后等待一点并抛出异常服务器丢弃的连接。

以下是代码(已解决https://java.net/projects/javamail/pages/OAuth2):

因为我在主线程上有它,所以我在onCreate方法中运行它:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    thread.start();//to run readMails();
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    ....
}
Thread thread = new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            readMails();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
public void readMails(){
    //it has been authenticated, so I am just getting token and user from authenticator
    String token=authPreferences.getToken();
    String user=authPreferences.getUser();

    System.out.println("Connecting to gmail with IMAP and OAUTH2.");

        String token=authPreferences.getToken();
        String user=authPreferences.getUser();

        Properties props = new Properties();
        props.put("mail.imaps.ssl.enable", "true");
        props.put("mail.imaps.sasl.enable", "true");
        props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
        props.put("mail.imaps.auth.login.disable", "true");
        props.put("mail.imaps.auth.plain.disable", "true");
        props.put("mail.debug.auth", "true");

try {            
        System.out.println("Creating session...");
        Session session = Session.getInstance(props);
        session.setDebug(true);
        System.out.println("Creating store...");
        Store mystore = session.getStore("imap");//s"
        System.out.println("Connecting store...");

        mystore.connect("imap.gmail.com", 993, user, token);
        //it does not get passed this - exception: connection dropped by server
        ...
}catch (Exception mex) {
            mex.printStackTrace();
            return;
        }
}

我尝试了不同版本的JavaMail(1.5.2,1.5.4) - 结果相同,我在验证时使用的范围是“https://mail.google.com”。

这是logcat:

08-25 22:03:52.907: I/System.out(28508): Connecting to gmail with IMAP and OAUTH2.
08-25 22:03:57.608: I/System.out(28508): Creating session...
08-25 22:03:57.683: I/System.out(28508): DEBUG: setDebug: JavaMail version 1.5.4
08-25 22:03:57.683: I/System.out(28508): Creating store...
08-25 22:03:57.684: I/System.out(28508): DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle,1.5.4]
08-25 22:03:57.685: W/art(28508): Before Android 4.1, method javax.mail.Session com.sun.mail.imap.IMAPStore.getSession() would have incorrectly overridden the package-private method in javax.mail.Service
08-25 22:03:57.688: I/System.out(28508): DEBUG IMAP: mail.imap.fetchsize: 16384
08-25 22:03:57.688: I/System.out(28508): DEBUG IMAP: mail.imap.ignorebodystructuresize: false
08-25 22:03:57.688: I/System.out(28508): DEBUG IMAP: mail.imap.statuscachetimeout: 1000
08-25 22:03:57.688: I/System.out(28508): DEBUG IMAP: mail.imap.appendbuffersize: -1
08-25 22:03:57.688: I/System.out(28508): DEBUG IMAP: mail.imap.minidletime: 10
08-25 22:03:57.689: I/System.out(28508): DEBUG IMAP: closeFoldersOnStoreFailure
08-25 22:03:57.689: I/System.out(28508): Connecting store...
08-25 22:03:59.011: I/System.out(28508): DEBUG IMAP: trying to connect to host "imap.gmail.com", port 993, isSSL false
08-25 22:04:14.994: W/System.err(28508): javax.mail.MessagingException: Connection dropped by server?;
08-25 22:04:14.994: W/System.err(28508):   nested exception is:
08-25 22:04:14.994: W/System.err(28508):    java.io.IOException: Connection dropped by server?
08-25 22:04:14.994: W/System.err(28508):    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:733)
08-25 22:04:14.994: W/System.err(28508):    at javax.mail.Service.connect(Service.java:364)
08-25 22:04:14.995: W/System.err(28508):    at bg.android.onemovesender.OneMoveSenderActivity.readMails(OneMoveSenderActivity.java:960)
08-25 22:04:14.995: W/System.err(28508):    at bg.android.onemovesender.OneMoveSenderActivity$1.run(OneMoveSenderActivity.java:914)
08-25 22:04:14.995: W/System.err(28508):    at java.lang.Thread.run(Thread.java:818)
08-25 22:04:14.995: W/System.err(28508): Caused by: java.io.IOException: Connection dropped by server?
08-25 22:04:14.995: W/System.err(28508):    at com.sun.mail.iap.ResponseInputStream.readResponse(ResponseInputStream.java:119)
08-25 22:04:14.995: W/System.err(28508):    at com.sun.mail.iap.Response.<init>(Response.java:114)
08-25 22:04:14.995: W/System.err(28508):    at com.sun.mail.imap.protocol.IMAPResponse.<init>(IMAPResponse.java:60)
08-25 22:04:14.995: W/System.err(28508):    at com.sun.mail.imap.protocol.IMAPProtocol.readResponse(IMAPProtocol.java:348) 
08-25 22:04:14.995: W/System.err(28508):    at com.sun.mail.iap.Protocol.<init>(Protocol.java:126)
08-25 22:04:14.995: W/System.err(28508):    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:125)
08-25 22:04:14.995: W/System.err(28508):    at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:754)
08-25 22:04:14.995: W/System.err(28508):    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:696)
08-25 22:04:14.995: W/System.err(28508):    ... 4 more

任何建议都会非常棒。感谢。

1 个答案:

答案 0 :(得分:0)

您正在设置“imaps”协议的属性,但您正在使用“imap”协议。将属性名称更改为“mail.imap。*”。