[英]Access user GMail account via IMAP using OAuth2 authentication in GAE Java application
这是一个复杂的情况,在GAE Java应用程序中,我具有具有https://mail.google.com/权限的OAuth2用户令牌。 该应用程序需要在用户GMail电子邮件中进行搜索,并按照以下示例( https://code.google.com/p/google-mail-oauth2-tools/wiki/JavaSampleCode )进行操作,我得到了一个有效的代码。 是的,可以,但是只能在GAE之外使用,我不能在其中使用com.sun.mail库。
ATM我的代码是:
public class OAuth2SaslAuthenticator {
private static final Logger logger =
Logger.getLogger(OAuth2SaslAuthenticator.class.getName());
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");
}
}
static void initialize() {
Security.addProvider(new OAuth2Provider());
}
public static Store connectToImap(String host,
int port,
String userEmail,
String oauthToken,
boolean debug) throws Exception {
Properties props = new Properties();
props.put("mail.store.protocol", "imaps");
props.put("mail.imap.host", host);
props.put("mail.imap.user", userEmail);
props.put("mail.imap.socketFactory", port);
props.put("mail.imap.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
props.put("mail.imap.port", port);
props.put("mail.imaps.sasl.enable", "true");
props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
props.put(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, oauthToken);
Session session = Session.getDefaultInstance(props);
session.setDebug(debug);
Store store = session.getStore();
store.connect();
return store;
}
}
其中OAuth2SaslClient和OAuth2SaslClient来自上述JavaSampleCode。
错误是javax.mail.NoSuchProviderException:imaps如果我注释imaps属性,则错误是:javax.mail.NoSuchProviderException:提供程序未设置
这对我有用:发生JAR文件冲突。 它是SMTP样式。
激活1.1.jar
import javax.activation.DataHandler;
邮件-1.5.0-b01.jar
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.util.ByteArrayDataSource;
import com.sun.mail.util.BASE64EncoderStream;
smtp-1.5.0.jar
import com.sun.mail.smtp.SMTPTransport;
和代码:
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 = Session.getInstance(props);
//session.setDebug(true);
final URLName unusedUrlName = null;
SMTPTransport transport = new SMTPTransport(session, unusedUrlName);
// If the password is non-null, SMTP tries to do AUTH LOGIN.
transport.connect("smtp.gmail.com", 587, userId, emptyPassword);
byte[] response = String.format("user=%s\1auth=Bearer %s\1\1", userId,
spreadsheet.accessToken).getBytes();
response = BASE64EncoderStream.encode(response);
transport.issueCommand("AUTH XOAUTH2 " + new String(response),
235);
MimeMessage message = new MimeMessage(session);
DataHandler handler = new DataHandler(new ByteArrayDataSource(messageBody.getBytes(), "text/plain"));
message.setSender(new InternetAddress(fromEmail));
message.setSubject(messageSubject);
message.setDataHandler(handler);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(messageToAddress));
transport.sendMessage(message, message.getAllRecipients());
System.out.println("SentTo:"+messageToAddress);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.