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

问题描述:

我使用Android的帐户管理器验证我的方式进入Gmail,因此我可以从设备发送电子邮件。无法使用Javamail和帐户管理器发送Gmail电子邮件(错误400)

在发送过程中,它出现(从日志),一切都连接并验证好了,但它给了我下面的错误:

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, [email protected], 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: [email protected] 
03-04 18:20:16.546 4847-5263/com.samay.markasread2 I/System.out﹕ IMAP SASL DEBUG: response: [email protected]=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 [email protected] 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 
+0

我不知道OAuth2令牌被认为是通过JavaMail使用的有效Gmail密码。 – CommonsWare

+0

@CommonsWare我正在使用此处指定的实现:http://*.com/questions/14682093/access-gmail-using-imap-with-accountmanager-token/14691708#comment21109529_14691708 – Rohan

+0

嘿@Rohan,是的,我得到了它使用这个代码工作:http://*.com/a/12821612/1692590。我想我得到了一个334错误,我也以为我是因为令牌已经过期,但事实并非如此。这种情况是我使用了错误的范围。但是,我刚刚检查了我的代码,并且我使用了与您相同的范围... – Jakob

的JavaMail 1.5出现完整的日志。 2将包括OAuth 2 support。您现在可以尝试SNAPSHOT release

+0

您是否可以在Android中看到示例实现? – Rohan

+0

对不起,不,Android不是Java。 –