Cannot connect to Gmail IMAP using XOAUTH on Android
Asked Answered
G

2

2

I'm building an app that uses Gmail to backup some data. I use XOAUTH to connect to Gmail and got the token and secret. But i could not connect to Gmail's IMAP service. I followed the example at 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;

When runs, it reports the following exception.

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

I used javamail of SUN. Anyone would help me ?

Gaffney answered 18/7, 2012 at 8:8 Comment(4)
Did you use the AccountManager to get the tokens? If so how did you get the secret token?Frostbitten
Did you enable debugging on the session? Is there anything output above where the exception was thrown?Frostbitten
How did you manage to get the 'oauthTokenSecret' on Android?Frostbitten
sorry for the delay, I used signpost to get the oauthToken and oauthTokenSecret.Gaffney
F
1

I am facing the same problems.

After enabling debugging (session.setDebug(true)) I noticed that I was receiving a ClassNotFoundException before the Tempory System Error was being shown.

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)

Looking through the JavaMail source code it would appear that this is because it is trying to load com.sun.mail.imap.protocol.IMAPSaslAuthenticator which has dependencies on non-android java resources.

javax.security.sasl.*
Frostbitten answered 6/8, 2012 at 14:29 Comment(7)
So what can we do to fix this?Gaffney
I am investigating alternative solutions at the moment and will report back any findings. Currently looking into implementing XOAUTH into K9 Mail as a proof of concept.Frostbitten
Hello there, kind of late to the party but I made sasl working with java mail for android. It was pretty easy, android (and java mail for android) just don't have the classes for sasl, javax.security.sasl.* and javax.security.auth.callback.* . I added the classes to java mail, changing namespace, modified some reference is the code and it's working with imap (didn't try with smtp because I have it already working skipping sasl). Not sure if it's the best way to do it, but well... Let me know if you are still interested, I'll post the code tomorrow.Prize
Hi alex: The way you mentioned works! I just added the lacking class and renamed it's package and it works fine. Thank you!!Gaffney
Hi @Gaffney Can you please reference how to add classes and renaming their package? The files extension is .class and are compiled so it's not possible to rename the package. Thanks in advance.Overburdensome
Hi @Tsikon,Since JavaMail is open-sourced, I'm sure you can find the missing classes' source code on the internet.Gaffney
Hi @Chris.Zou, I'm having trouble figuring out how to do it, can you please give me some tips or step by step short tutorial for accomplishing this or even if you could send me the jar you created that would be great. Any help would be appreciated :). Thanks!Overburdensome
V
0

Other people also experienced this error.

Try the suggestions they are giving:

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

and

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.

Vite answered 18/7, 2012 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.