I use code from this link to access gmail imap server, because I could not find Android-friendly port of javamail with OAUTH support (javamail 1.5.2 or higher).
However, the problem with this code:
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(OAuth2SaslClientFactory.OAUTH_TOKEN_PROP, 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;
}
is that a new Store object is created every time auth token is changed (expires). And then I have to create a new Folder and read my messages again...
My question is:
Is it possible to change auth token without creating a new Store
object? I would like to be able to implement something like
store.connect("imap.gmail.com", username, oauth2_access_token)
(example from javamail 1.5.2) to reconnect, without the need to recreate the Store
object.
Thank you very much!
If you need to create a new connection with the same Store you should be able to set the property to a new value and make a new connection, without creating a new Store object. Just call props.put with the new value. The Session keeps a reference to the Properties object rather than making a copy of it.