This is a complex situation, inside a GAE Java application I have the OAuth2 user token with the https://mail.google.com/ permission. The application needs to search in the user GMail emails and following this example (https://code.google.com/p/google-mail-oauth2-tools/wiki/JavaSampleCode) I got a working code. Working, yes, but only outside GAE, where I cannot use the com.sun.mail libraries.
ATM my code is:
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;
}
}
where the OAuth2SaslClient and the OAuth2SaslClient come from the above JavaSampleCode.
The error is javax.mail.NoSuchProviderException: imaps And if I comment the imaps property the error is: javax.mail.NoSuchProviderException: provider is not set
This worked for me: There was a JAR file clash. It is SMTP style.
activation-1.1.jar
import javax.activation.DataHandler;
mail-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;
And code:
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);