Android的JavaMail的自定义身份验证

问题描述:

所以,我认识到,SASL没有在Android的实施,并且因此了JavaMail的oauth2方法不会开箱。

So, I realize that SASL is not implemented in android, and that consequently the javamail oauth2 methods won't out of the box.

不过,根据对IMAP的Gmail谷歌的API文档( https://开头的开发。 google.com/google-apps/gmail/xoauth2_protocol )这似乎相当琐碎得到一个身份验证令牌,连接code它,并发送AUTHENTICATE XOAUTH2手动服务器。

However, according to the google api docs for gmail imap (https://developers.google.com/google-apps/gmail/xoauth2_protocol) it would seem fairly trivial to get an auth token, encode it, and send the 'AUTHENTICATE XOAUTH2 ' to the server manually.

我的问题:我想使用JavaMail IMAP功能(不想处理IMAP协议命令和响应改写(munging)我自己),但我需要一种方法来简单地插入正确的验证命令 - 不是传统的登录用户名密码。

My problem: I want to use the javamail IMAP functions (don't want to handle IMAP protocol commands and response munging myself), but I need a way to simply insert the proper authentication command - not the traditional login username password.

可以这样做,这将是去做最简单的方法?我可以创建自己的身份验证方法,轻松地实现这一点?

Can this be done and what would be the easiest way to go about it? Could I create my own authentication method and implement this easily?

另外,我可以创建连接,然后用手这一关到IMAPStore(怀疑这会工作,因为它创建和管理一个连接池)。

Alternatively, can I create the connection and then hand this off to an IMAPStore (doubt this would work as it creates and manages a connection pool).

---编辑---

只是为了更具体地,所述javax.security.sasl中方法没有的Andr​​oid其中Imap.protocol.IMAPSaslAuthenticator依赖于实现的。

Just to be more specific, the javax.security.sasl method is not implemented in Android which Imap.protocol.IMAPSaslAuthenticator relies on.

我有IMAP的SASL支持工作的jar(用于SMTP,你必须发出命令如你所说,因为Android的Java邮件不具备SASL基础设施SMTP)。
我所做的是包javax.security.sasl中和javax.security.auth.callback中添加到Java邮件Android源和重新包装。我不得不把在不同的namaspace,myjavax.security.sasl等。否则的Andr​​oid构建系统抱怨包包含核心库,不知道为什么它不适合javax.mail这是一个核心的命名空间反正抱怨(使用javax)。我也不得不改变一些进口的部分班级新的命名空间,但基本上都是packeges类本身和com.sun.mail.imap.protocol.IMAPSaslAuthenticator。
有了这个,我可以成功进行身份验证使用的OAuth 2令牌我从Android的AccountManager得到了Gmail的IMAP服务器,我没有尝试别的。对于SMTP,你可以看看我的答案在这里Javamail API在Android中使用XOAUTH
我认为这是可能使SMTP工作,SASL从这里http://kenai.com/projects/javamail/sources/mercurial/show/mail/src/main/java/com/sun/mail/smtp其中有SMTP SASL支持。
我认为这是完全正常的,因为我做的是增加一些GPL类,我添加的是从JDK。
希望这会有所帮助,如果你需要的罐子或者对如何做一些更多的解释,只是问。

I have a working jar with sasl support for imap (for smtp you have to issue the command as you said because java mail for android doesn't have the sasl infrastructure for smtp). All I did was to add the packages javax.security.sasl and javax.security.auth.callback to the java mail for android source and repackage it. I had to put the in a different namaspace, myjavax.security.sasl etc. because otherwise android build system complains that the package contains core libraries, not sure why it doesn't complain for javax.mail which is in a core namespace anyway (javax). I also had to change some import for the new namespace in some classes, but basically are the packeges classes themselves and com.sun.mail.imap.protocol.IMAPSaslAuthenticator. With this I could successfully authenticate to gmail imap server using the oauth 2 token I got from the Android AccountManager, I didn't try anything else. For smtp, you can look at my answer here Javamail api in android using XOauth I think it's possible to make smtp work with Sasl getting the new classes from here http://kenai.com/projects/javamail/sources/mercurial/show/mail/src/main/java/com/sun/mail/smtp which have Sasl support for smtp. I think this is perfectly fine because all I do is adding some GPL classes, the one that I added are from the JDK. Hope this helps, if you need the jars or some more explanation on how to do it, just ask.