Unable to send gmail e-mail using Javamail and Acc

2019-09-01 10:00发布

问题:

I am using Android's Account Manager to authenticate my way into Gmail so I can send an e-mail from the device.

During the send process, it appears (from the logs) that everything connects and authenticates okay, but it gives me the following error:

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

Which when decoded reads:

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

From other SO questions, I've seen that this means that the token is expired. However, this is NOT the case in my case, I have tried invalidating the token and getting a new one; same problem.

Here's how I authenticate:

 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);
        }

Here's the full log which appears after I click to send a test e-mail message to myself (note authentication success)

    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:

JavaMail 1.5.2 will include OAuth 2 support. You can try the SNAPSHOT release now.