Skip to content

Instantly share code, notes, and snippets.

@Abhi-Codes
Created October 18, 2022 15:58
Show Gist options
  • Save Abhi-Codes/99242623c3c8e433e295c3edd2ea5444 to your computer and use it in GitHub Desktop.
Save Abhi-Codes/99242623c3c8e433e295c3edd2ea5444 to your computer and use it in GitHub Desktop.
Connect to mailbox using IMAP Oauth2
public String getAccessTokenByClientCredentialGrant() {
String accessToken = null;
String clientId = imapClientId;
String secret = imapSecret;
String authority = "https://login.microsoftonline.com/" + imapTenantId + "/oauth2/v2.0/token";
String scope = "https://outlook.office365.com/.default";
log.info("Client ID : " + clientId);
log.info("Client Secret : " + secret);
log.info("Auth Server: " + authority);
log.info("Scope: " + scope);
try {
ConfidentialClientApplication app = ConfidentialClientApplication
.builder(clientId, ClientCredentialFactory.createFromSecret(secret)).authority(authority).build();
// With client credentials flows the scope is ALWAYS of the shape
// "resource/.default", as the
// application permissions need to be set statically (in the portal), and then
// granted by a tenant administrator
ClientCredentialParameters clientCredentialParam = ClientCredentialParameters
.builder(Collections.singleton(scope)).build();
CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
IAuthenticationResult result = future.get();
accessToken = result.accessToken();
} catch (Exception e) {
log.error("Exception in acquiring token: " + e.getMessage());
e.printStackTrace();
}
log.info("Access Token : " + accessToken);
return accessToken;
}
// This method connects to store using the access token
public Store connectViaOauth(String userEmailId, String oauth2AccessToken) throws Exception {
Store store = null;
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties();
props.put("mail.imaps.ssl.enable", "true");
props.put("mail.imaps.sasl.enable", "true");
props.put("mail.imaps.port", imapPort);
props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
props.put("mail.imaps.auth.login.disable", "true");
props.put("mail.imaps.auth.plain.disable", "true");
props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imaps.socketFactory.fallback", "false");
props.setProperty("mail.imaps.socketFactory.port", imapPort);
props.setProperty("mail.imaps.starttls.enable", "true");
props.put("mail.debug", "true");
props.put("mail.debug.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
store = session.getStore("imaps");
log.info("OAUTH2 IMAP trying to connect with system properties to Host:" + imapHost + ", Port: " + imapPort
+ ", userEmailId: " + userEmailId + ", AccessToken: " + oauth2AccessToken);
try {
store.connect(imapHost, userEmailId, oauth2AccessToken);
log.info("IMAP connected with system properties to Host:" + imapHost + ", Port: " + imapPort
+ ", userEmailId: " + userEmailId + ", AccessToken: " + oauth2AccessToken);
if (store.isConnected()) {
log.info("Connection Established using imap protocol successfully !");
}
} catch (Exception e) {
log.error("Store.Connect failed with the errror: " + e.getMessage());
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
log.error(exceptionAsString);
}
return store;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment