有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法在Android上使用XOAUTH连接到Gmail IMAP

我正在开发一个应用程序,使用Gmail备份一些数据。我使用XOAUTH连接到Gmail并获得了令牌和密码。但我无法连接到Gmail的IMAP服务。我遵循了http://code.google.com/p/google-mail-xoauth-tools/wiki/JavaSampleCode的示例:

Properties props = new Properties();
props.put("mail.imaps.sasl.enable", "true");
props.put("mail.imaps.sasl.mechanisms", "XOAUTH");
props.put(OAUTH_TOKEN_PROP, oauthToken);
props.put(OAUTH_TOKEN_SECRET_PROP, oauthTokenSecret);
props.put(CONSUMER_KEY_PROP, Const.CONSUMER_KEY);
props.put(CONSUMER_SECRET_PROP, Const.CONSUMER_SECRET);
Session session = Session.getInstance(props);
session.setDebug(debug);

final URLName unusedUrlName = null;
IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlName);
final String emptyPassword = "";
store.connect(host, port, userEmail, emptyPassword);
return store;

运行时,它会报告以下异常

javax.mail.MessagingException: * BYE [UNAVAILABLE] Temporary System Error;
   nested exception is:
    com.sun.mail.iap.ConnectionException: * BYE [UNAVAILABLE] Temporary System Error
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:569)
    at javax.mail.Service.connect(Service.java:288)
    at com.doodlemobile.zy.finenote.OAuthHelperActivity$XoauthAuthenticator.connectToImap(OAuthHelperActivity.java:565)
    at com.doodlemobile.zy.finenote.OAuthHelperActivity$ConnectGmailTask.doInBackground(OAuthHelperActivity.java:484)
    at com.doodlemobile.zy.finenote.OAuthHelperActivity$ConnectGmailTask.doInBackground(OAuthHelperActivity.java:1)
    at 安卓.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at 安卓.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    at java.lang.Thread.run(Thread.java:856)
 Caused by: com.sun.mail.iap.ConnectionException: * BYE [UNAVAILABLE] Temporary System Error
    at com.sun.mail.iap.Protocol.handleResult(Protocol.java:346)
    at com.sun.mail.imap.protocol.IMAPProtocol.login(IMAPProtocol.java:336)
    at com.sun.mail.imap.IMAPStore.login(IMAPStore.java:615)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:549)
    ... 11 more

我使用了SUN的javamail。有人愿意帮助我吗


共 (2) 个答案

  1. # 1 楼答案

    我也面临同样的问题

    启用调试(session.setDebug(true))后,我注意到在显示临时系统错误之前,我收到了一个^{}

    08-06 14:38:59.148: I/System.out(3139): DEBUG IMAP: Can't load SASL authenticator: java.lang.ClassNotFoundException: com.sun.mail.imap.protocol.IMAPSaslAuthenticator
    08-06 14:38:59.148: I/System.out(3139): DEBUG IMAP: LOGIN command trace suppressed
    08-06 14:39:00.265: I/System.out(3139): DEBUG IMAP: LOGIN command result: * BYE [UNAVAILABLE] Temporary System Error
    08-06 14:39:00.289: E/com.example.gmailandroid.MainActivity(3139): * BYE [UNAVAILABLE] Temporary System Error
    08-06 14:39:00.289: E/com.example.gmailandroid.MainActivity(3139): javax.mail.MessagingException: * BYE [UNAVAILABLE] Temporary System Error;
    08-06 14:39:00.289: E/com.example.gmailandroid.MainActivity(3139):   nested exception is:
    08-06 14:39:00.289: E/com.example.gmailandroid.MainActivity(3139):  com.sun.mail.iap.ConnectionException: * BYE [UNAVAILABLE] Temporary System Error
    08-06 14:39:00.289: E/com.example.gmailandroid.MainActivity(3139):  at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:668)
    

    通过查看JavaMail源代码,似乎这是因为它试图加载com。太阳邮政imap。协议IMAPSaslAuthenticator,它依赖于非android java资源

    javax.security.sasl.*
    
  2. # 2 楼答案

    其他人也experienced this error

    尝试他们给出的建议:

    I unregistered, then registered the security provider between each session, and it dermed to fix it. No idea why though...

    I did end up getting it working. It turns out I needed a newer version of Java on the system I was working with. Once I upgraded things started working without issue.