无法使用Javamail和客户经理发送Gmail电子邮件(错误400)

时间:2014-03-04 23:30:42

标签: android oauth smtp javamail accountmanager

我正在使用Android的客户经理验证我进入Gmail的方式,以便我可以从设备发送电子邮件。

在发送过程中,(从日志中)显示所有内容都连接并进行身份验证,但它会给我以下错误:

E/SMTP TRANSPORT ERROR﹕ 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
    class javax.mail.MessagingException

解码时读取:

{"status":"400","schemes":"Bearer","scope":"https://mail.google.com/"}

从其他SO问题来看,我发现这意味着令牌已过期。但是,在我的情况下情况并非如此,我已经尝试使令牌失效并获得新令牌;同样的问题。

以下是我的身份验证方式:

 am = AccountManager.get(this);




        options = new Bundle();
        Account me = null;

        Account[] accounts = am.getAccounts();
        for(Account a: accounts){
            if(a.name.equals(TEST_ACCOUNT_EMAIL) && a.type.equals("com.google")){
                me = a;
            }
        }
        if (me != null){
            am.getAuthToken(me, "oauth2:https://mail.google.com/", null, this, new OnTokenAcquired(), null);
        }

这是我点击向自己发送测试电子邮件后显示的完整日志(注意身份验证成功)

    03-04 18:20:15.875    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG: setDebug: JavaMail version 1.4.1
03-04 18:20:15.875    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG: mail.imap.fetchsize: 16384
03-04 18:20:15.885    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG: enable SASL
03-04 18:20:15.885    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG: SASL mechanisms allowed: XOAUTH2
03-04 18:20:16.175    4847-4859/com.samay.markasread2 I/System.out﹕ A3 LOGOUT
03-04 18:20:16.175    4847-4859/com.samay.markasread2 I/System.out﹕ DEBUG: IMAPStore connection dead
03-04 18:20:16.175    4847-4859/com.samay.markasread2 I/System.out﹕ DEBUG: IMAPStore cleanup, force true
03-04 18:20:16.175    4847-4859/com.samay.markasread2 I/System.out﹕ DEBUG: IMAPStore cleanup done
03-04 18:20:16.185    4847-4859/com.samay.markasread2 I/System.out﹕ A3 LOGOUT
03-04 18:20:16.336    4847-4859/com.samay.markasread2 I/System.out﹕ * BYE LOGOUT Requested
03-04 18:20:16.336    4847-4859/com.samay.markasread2 I/System.out﹕ A3 OK 73 good day (Success)
03-04 18:20:16.336    4847-4859/com.samay.markasread2 I/System.out﹕ DEBUG: IMAPStore connection dead
03-04 18:20:16.336    4847-4859/com.samay.markasread2 I/System.out﹕ DEBUG: IMAPStore cleanup, force false
03-04 18:20:16.336    4847-4859/com.samay.markasread2 I/System.out﹕ DEBUG: IMAPStore cleanup done
03-04 18:20:16.346    4847-5263/com.samay.markasread2 I/System.out﹕ * OK Gimap ready for requests from 18.111.30.155 67mb91143115qga
03-04 18:20:16.346    4847-5263/com.samay.markasread2 I/System.out﹕ A0 CAPABILITY
03-04 18:20:16.446    4847-5263/com.samay.markasread2 I/System.out﹕ * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH AUTH=XOAUTH2 AUTH=PLAIN AUTH=PLAIN-CLIENTTOKEN
03-04 18:20:16.446    4847-5263/com.samay.markasread2 I/System.out﹕ A0 OK Thats all she wrote! 67mb91143115qga
03-04 18:20:16.446    4847-5263/com.samay.markasread2 I/System.out﹕ IMAP DEBUG: AUTH: XOAUTH
03-04 18:20:16.446    4847-5263/com.samay.markasread2 I/System.out﹕ IMAP DEBUG: AUTH: XOAUTH2
03-04 18:20:16.446    4847-5263/com.samay.markasread2 I/System.out﹕ IMAP DEBUG: AUTH: PLAIN
03-04 18:20:16.446    4847-5263/com.samay.markasread2 I/System.out﹕ IMAP DEBUG: AUTH: PLAIN-CLIENTTOKEN
03-04 18:20:16.446    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG: protocolConnect login, host=imap.gmail.com, user=@gmail.com, password=<non-null>
03-04 18:20:16.446    4847-5263/com.samay.markasread2 I/System.out﹕ IMAP SASL DEBUG: Mechanisms: XOAUTH2
03-04 18:20:16.456    4847-5263/com.samay.markasread2 I/System.out﹕ IMAP SASL DEBUG: SASL client XOAUTH2
03-04 18:20:16.456    4847-5263/com.samay.markasread2 I/System.out﹕ A1 AUTHENTICATE XOAUTH2
03-04 18:20:16.546    4847-5263/com.samay.markasread2 I/System.out﹕ +
03-04 18:20:16.546    4847-5263/com.samay.markasread2 I/System.out﹕ IMAP SASL DEBUG: challenge:  :
03-04 18:20:16.546    4847-5263/com.samay.markasread2 I/System.out﹕ IMAP SASL DEBUG: callback length: 1
03-04 18:20:16.546    4847-5263/com.samay.markasread2 I/System.out﹕ IMAP SASL DEBUG: callback 0: myjavax.security.auth.callback.NameCallback@64df4000
03-04 18:20:16.546    4847-5263/com.samay.markasread2 I/System.out﹕ IMAP SASL DEBUG: response: user=CENSOREDEMAIL@gmail.comauth=Bearer ya29.1.AADtN_UU_dI1wnX8uehx7yiIHeeI3oOTl1m5Pj3jyEZHtjpeCalTuSQvfbbhtYsG57H7IPZTDcWO5WwAKta8KvP3xYmPV3A6YbLCEBG15MDVUksRL9mMuLqH9u0 :
03-04 18:20:16.546    4847-5263/com.samay.markasread2 I/System.out﹕ dXNlcj1wdXJpcm9oYW5AZ21haWwuY29tAWF1dGg9QmVhcmVyIHlhMjkuMS5BQUR0Tl9VVV9kSTF3blg4dWVoeDd5aUlIZWVJM29PVGwxbTVQajNqeUVaSHRqcGVDYWxUdVNRdmZiYmh0WXNHNTdIN0lQWlREY1dPNVd3QUt0YThLdlAzeFltUFYzQTZZYkxDRUJHMTVNRFZVa3NSTDltTXVMcUg5dTABAQ==
03-04 18:20:16.756    4847-5263/com.samay.markasread2 I/System.out﹕ * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH
03-04 18:20:16.756    4847-5263/com.samay.markasread2 I/System.out﹕ A1 OK CENSOREDEMAIL@gmail.com CENSORED_FIRST_NAME CENSORED_LAST_NAME authenticated (Success)
03-04 18:20:16.756    4847-5263/com.samay.markasread2 I/System.out﹕ A2 CAPABILITY
03-04 18:20:16.856    4847-5263/com.samay.markasread2 I/System.out﹕ * CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 UIDPLUS COMPRESS=DEFLATE ENABLE MOVE CONDSTORE ESEARCH
03-04 18:20:16.856    4847-5263/com.samay.markasread2 I/System.out﹕ A2 OK Success
03-04 18:20:16.896    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG: setDebug: JavaMail version 1.4.1
03-04 18:20:16.896    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: useEhlo true, useAuth false
03-04 18:20:16.896    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
03-04 18:20:17.176    4847-5263/com.samay.markasread2 I/System.out﹕ 220 mx.google.com ESMTP r40sm644209qga.23 - gsmtp
03-04 18:20:17.176    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
03-04 18:20:17.176    4847-5263/com.samay.markasread2 I/System.out﹕ EHLO localhost
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ 250-mx.google.com at your service, [18.111.30.155]
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ 250-SIZE 35882577
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ 250-8BITMIME
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ 250-STARTTLS
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ 250-ENHANCEDSTATUSCODES
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ 250 CHUNKING
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: Found extension "SIZE", arg "35882577"
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: Found extension "8BITMIME", arg ""
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: Found extension "STARTTLS", arg ""
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: Found extension "CHUNKING", arg ""
03-04 18:20:17.277    4847-5263/com.samay.markasread2 I/System.out﹕ STARTTLS
03-04 18:20:17.387    4847-5263/com.samay.markasread2 I/System.out﹕ 220 2.0.0 Ready to start TLS
03-04 18:20:17.597    4847-5263/com.samay.markasread2 I/System.out﹕ EHLO localhost
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ 250-mx.google.com at your service, [18.111.30.155]
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ 250-SIZE 35882577
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ 250-8BITMIME
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ 250-ENHANCEDSTATUSCODES
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ 250 CHUNKING
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: Found extension "SIZE", arg "35882577"
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: Found extension "8BITMIME", arg ""
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN"
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ DEBUG SMTP: Found extension "CHUNKING", arg ""
03-04 18:20:17.697    4847-5263/com.samay.markasread2 I/System.out﹕ AUTH XOAUTH2 dXNlcj1zZW5kLWFkZHJAZ21haWwuY29tAWF1dGg9QmVhcmVyIHlhMjkuMS5BQUR0Tl9VVV9kSTF3blg4dWVoeDd5aUlIZWVJM29PVGwxbTVQajNqeUVaSHRqcGVDYWxUdVNRdmZiYmh0WXNHNTdIN0lQWlREY1dPNVd3QUt0YThLdlAzeFltUFYzQTZZYkxDRUJHMTVNRFZVa3NSTDltTXVMcUg5dTABAQ==
03-04 18:20:18.007    4847-5263/com.samay.markasread2 I/System.out﹕ 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
03-04 18:20:18.007    4847-5263/com.samay.markasread2 E/SMTP TRANSPORT ERROR﹕ 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
    class javax.mail.MessagingException

1 个答案:

答案 0 :(得分:1)

JavaMail 1.5.2将包含OAuth 2 support。您现在可以尝试SNAPSHOT release