我正在尝试通过用户帐户发送电子邮件(无需插入用户,密码),使用Oauth2进行连接。
这是我获取访问令牌的代码:
String SCOPES = "https://mail.google.com/";
token = GoogleAuthUtil.getToken(m_context,SystemUtills.getUsername(m_context)+"@gmail.com","oauth2:" + SCOPES);
这很好用,给了我一个代币。
现在我正试图像这样连接到imap和smtp:
public class OAuth2Authenticator extends AsyncTask<String, Void, String> {
private static final Logger logger =Logger.getLogger(OAuth2Authenticator.class.getName());
private static String user_email;
public static final class OAuth2Provider extends Provider {
private static final long serialVersionUID = 1L;
public OAuth2Provider() {
super("Google OAuth2 Provider", 1.0,
"Provides the XOAUTH2 SASL Mechanism");
put("SaslClientFactory.XOAUTH2",
"com.google.code.samples.oauth2.OAuth2SaslClientFactory");
}
}
public static void initialize() {
Security.addProvider(new OAuth2Provider());
}
public static IMAPStore connectToImap(String host,
int port,
String userEmail,
String oauthToken,
boolean debug) throws Exception {
Properties props = new Properties();
props.put("mail.imaps.sasl.enable", "true");
props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken", oauthToken);
Session session = Session.getInstance(props);
session.setDebug(debug);
final URLName unusedUrlName = null;
IMAPSSLStore store = new IMAPSSLStore(session, unusedUrlName);
final String emptyPassword = "";
store.connect(host, port, userEmail, emptyPassword);
return store;
}
public static 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", "true");
props.put("mail.smtp.sasl.mechanisms", "XOAUTH2");
props.put("mail.imaps.sasl.mechanisms.oauth2.oauthToken", oauthToken);
Session 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 = "";
transport.connect(host, port, userEmail, emptyPassword);
return transport;
}
@Override
protected String doInBackground(String... arg0)
{
try
{
IMAPStore imapStore = connectToImap("imap.gmail.com", 993, AppSettings.user_email,AppSettings.getAccessToken(), true);
SMTPTransport smtpTransport = connectToSmtp("smtp.gmail.com",587,AppSettings.user_email,AppSettings.getAccessToken(),true);
return "SUC";
}catch (Exception e) {
e.printStackTrace();
return "FLD";
}
}
在我的调用类中,我使用:
调用连接new AsyncGetAccessToken().execute();
OAuth2Authenticator.initialize();
try{
new OAuth2Authenticator().execute();
}catch (Exception e) {
e.printStackTrace();
}
对于smtp(调试协商:
04-01 07:57:08.770: I/System.out(31190): DEBUG: setDebug: JavaMail version 1.4.1
04-01 07:57:37.890: I/System.out(31190): DEBUG SMTP: useEhlo true, useAuth false
04-01 07:57:37.900: I/System.out(31190): DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
04-01 07:57:38.150: I/System.out(31190): 220 mx.google.com ESMTP q41sm37770244eez.7 - gsmtp
04-01 07:57:38.150: I/System.out(31190): DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
04-01 07:57:38.155: I/System.out(31190): EHLO localhost
04-01 07:57:38.310: I/System.out(31190): 250-mx.google.com at your service, [46.120.129.170]
04-01 07:57:38.310: I/System.out(31190): 250-SIZE 35882577
04-01 07:57:38.320: I/System.out(31190): 250-8BITMIME
04-01 07:57:38.320: I/System.out(31190): 250-STARTTLS
04-01 07:57:38.320: I/System.out(31190): 250-ENHANCEDSTATUSCODES
04-01 07:57:38.320: I/System.out(31190): 250 CHUNKING
04-01 07:57:38.330: I/System.out(31190): DEBUG SMTP: Found extension "SIZE", arg "35882577"
04-01 07:57:38.330: I/System.out(31190): DEBUG SMTP: Found extension "8BITMIME", arg ""
04-01 07:57:38.330: I/System.out(31190): DEBUG SMTP: Found extension "STARTTLS", arg ""
04-01 07:57:38.335: I/System.out(31190): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
04-01 07:57:38.335: I/System.out(31190): DEBUG SMTP: Found extension "CHUNKING", arg ""
04-01 07:57:38.345: I/System.out(31190): STARTTLS
04-01 07:57:38.440: I/System.out(31190): 220 2.0.0 Ready to start TLS
04-01 07:57:39.115: I/System.out(31190): EHLO localhost
04-01 07:57:39.220: I/System.out(31190): 250-mx.google.com at your service, [46.120.129.170]
04-01 07:57:39.220: I/System.out(31190): 250-SIZE 35882577
04-01 07:57:39.220: I/System.out(31190): 250-8BITMIME
04-01 07:57:39.225: I/System.out(31190): 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
04-01 07:57:39.225: I/System.out(31190): 250-ENHANCEDSTATUSCODES
04-01 07:57:39.225: I/System.out(31190): 250 CHUNKING
04-01 07:57:39.235: I/System.out(31190): DEBUG SMTP: Found extension "SIZE", arg "35882577"
04-01 07:57:39.235: I/System.out(31190): DEBUG SMTP: Found extension "8BITMIME", arg ""
04-01 07:57:39.240: I/System.out(31190): DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN"
04-01 07:57:39.240: I/System.out(31190): DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
04-01 07:57:39.245: I/System.out(31190): DEBUG SMTP: Found extension "CHUNKING", arg ""
04-01 07:57:39.245: I/System.out(31190): DEBUG SMTP: Attempt to authenticate
04-01 07:57:39.250: I/System.out(31190): AUTH LOGIN
04-01 07:57:39.350: I/System.out(31190): 334 VXNlcm5hbWU6
04-01 07:57:39.355: I/System.out(31190): bWljaGFlbC5hc2FyYWZAZ21haWwuY29t
04-01 07:57:39.460: I/System.out(31190): 334 UGFzc3dvcmQ6
04-01 07:57:39.470: I/System.out(31190):
04-01 07:57:39.990: I/System.out(31190): 535-5.7.8 Username and Password not accepted. Learn more at
04-01 07:57:39.990: I/System.out(31190): 535 5.7.8 http://support.google.com/mail/bin/answer.py?answer=14257 q41sm37770244eez.7 - gsmtp
例外:
04-01 08:00:57.860: W/System.err(31190): javax.mail.AuthenticationFailedException
04-01 08:00:57.880: W/System.err(31190): at javax.mail.Service.connect(Service.java:319)
04-01 08:00:57.885: W/System.err(31190): at com.my_android_assistant.OAUTH.OAuth2Authenticator.connectToSmtp(OAuth2Authenticator.java:128)
答案 0 :(得分:1)
它没有使用OAuth2提供程序,因为服务器通告LOGIN,因此JavaMail首先选择它。您需要禁用LOGIN和PLAIN机制以强制它使用SASL并选择XOAUTH2。将“mail.imaps.auth.login.disable”和“mail.imaps.auth.plain.disable”设置为“true”。
此外,您可能想尝试OAuth2 support built into JavaMail 1.5.2。 SNAPSHOT release可用。