2012-07-18 115 views
2

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

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 android.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 android.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。任何人都可以帮助我?

+0

您是否使用AccountManager获取令牌?如果是的话,你是如何得到这个秘密标记的? – Malachi 2012-08-01 16:12:29

+0

您是否在会话中启用调试?有什么输出上面的例外抛出? – Malachi 2012-08-02 14:08:18

+0

你是如何设法在Android上获得'oauthTokenSecret'的? – Malachi 2012-08-09 14:24:55

回答

1

我面临同样的问题。

启用调试(session.setDebug(true))后,我注意到在显示暂存系统错误之前我收到ClassNotFoundException

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.sun.mail.imap.protocol.IMAPSaslAuthenticator这对非Android的Java资源的依赖。

javax.security.sasl.* 
+0

那么我们能做些什么来解决这个问题呢? – 2012-08-07 15:07:27

+0

我现在正在调查其他解决方案,并会报告任何调查结果。目前正在研究将XOAUTH实现为K9 Mail作为概念验证。 – Malachi 2012-08-09 10:43:37

+3

你好,有点晚了派对,但我做了sasl与Android的Java邮件工作。这很容易,android(和java的邮件为android)只是没有sasl,javax.security.sasl。*和javax.security.auth.callback。*的类。我将类添加到java邮件,更改命名空间,修改了一些参考代码,它与imap一起工作(没有用smtp尝试,因为我已经在使用sasl了)。不知道这是否是最好的方式,但是,如果您仍然对此感兴趣,请告诉我,明天我会发布代码。 – alex 2012-11-24 04:01:14

0

其他人也experienced this error

尝试他们给的建议:

我注销,然后注册的每个会话的安全提供者,它dermed修复它。不知道为什么,虽然...

我最终得到它的工作。事实证明,我正在使用的系统上需要更新版本的Java。一旦我升级的东西开始工作没有问题。

+0

这不适合我。谢谢你们一切。 – 2012-09-28 09:36:22

相关问题