I am getting following error when I try to run my android project after adding new AUTH XOAUTH2 command.I am using android device with os version 2.3 but same code is working fine on android 4.0
public SMTPTransport connectToSmtp(String host, int port, String userEmail,String oauthToken, boolean debug) throws Exception
{
Properties props = new Properties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
props.put("mail.smtp.sasl.enable", "false");
session = Session.getInstance(props);
session.setDebug(debug);
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
final String emptyPassword = null;
transport.connect(host, port, userEmail, emptyPassword);
byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", userEmail,oauthToken).getBytes();
response = BASE64EncoderStream.encode(response);
transport.issueCommand("AUTH XOAUTH2 " + new String(response),235);
return transport;
}
Please check the logs
03-21 10:05:08.679: W/System.err(987): javax.mail.MessagingException: 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
03-21 10:05:08.679: W/System.err(987): at com.sun.mail.smtp.SMTPTransport.issueCommand(SMTPTransport.java:1481)
03-21 10:05:08.679: W/System.err(987): at com.swapmeen.test.GMailOauthSender.connectToSmtp(GMailOauthSender.java:48)
03-21 10:05:08.679: W/System.err(987): at com.swapmeen.test.GMailOauthSender.sendMail(GMailOauthSender.java:57)
03-21 10:05:08.679: W/System.err(987): at com.swapmeen.test.MainActivity$1.run(MainActivity.java:64)
The error you are receiving
eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
decodes to the following using a Base64 Decoder
{"status":"400","schemes":"Bearer","scope":"https://mail.google.com/"}
What this ends up meaning (as cryptic as the message is) is that the authentication token that you are using has expired. You need to invalidate it and then get a new one (simply request the token again).
You invalidate the token like this:
mAccountManager.invalidateAuthToken("com.google", mAuthenticationToken);
I hope that this is helpful :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With