This is my first time asking a question here. If anything is missing, please let me know
I have a Spring Boot application, and I want this application to read Outlook emails using IMAP. For this, I registered the application in Azure AD, and I am receiving the token. The token also contains the permissions. However, when I try to run the code below with this token, I get the error “A1 NO AUTHENTICATE failed.”
Below are the Spring Boot code, YAML file, and log.
I would like to know where I might be going wrong.
spring:
security:
oauth2:
client:
registration:
azure:
client-id: *****
client-secret: ******
authorization-grant-type: authorization_code
scope: "https://graph.microsoft.com/.default"
redirect-uri: "http://localhost:8080/login/oauth2/code/azure"
provider: azure
provider:
azure:
authorization-uri: https://login.microsoftonline.com/common/oauth2/v2.0/authorize
token-uri: https://login.microsoftonline.com/common/oauth2/v2.0/token
user-info-uri: https://graph.microsoft.com/oidc/userinfo
user-name-attribute: sub
public void connect(String userEmailId, String oauth2AccessToken) {
String host = "outlook.office365.com";
String port = "993";
Store store = null;
Properties props = new Properties();
props.put("mail.imap.ssl.enable", "true");
props.put("mail.imap.sasl.enable", "true");
props.put("mail.imap.port", port);
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
props.put("mail.imap.sasl.mechanisms", "XOAUTH2");
props.put("mail.imap.auth.login.disable", "true");
props.put("mail.imap.auth.plain.disable", "true");
props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imap.socketFactory.fallback", "false");
props.setProperty("mail.imap.socketFactory.port", port);
props.setProperty("mail.imap.starttls.enable", "true");
props.put("mail.debug", "true");
props.put("mail.debug.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
try {
store = session.getStore("imap");
store.connect(host, userEmailId, oauth2AccessToken);
log.info("IMAP connected successfully!");
if (store.isConnected()) {
log.info("Connection Established successfully!");
}
} catch (Exception e) {
log.error("Store.Connect failed with the error: ", e);
} finally {
if (store != null && store.isConnected()) {
try {
store.close();
} catch (MessagingException e) {
log.error("Failed to close store connection: ", e);
}
}
}
}
@GetMapping("/test")
public void test(@RegisteredOAuth2AuthorizedClient("azure") OAuth2AuthorizedClient authorizedClient) throws Exception {
String accessToken = authorizedClient.getAccessToken().getTokenValue();
emailBasicAuthService.connect("[email protected]",accessToken);
}
A1 NO AUTHENTICATE failed.
10:45:03.250 [http-nio-8080-exec-8] ERROR c.e.service.EmailBasicAuthService 114 – Store.Connect failed with the error: AUTHENTICATE failed.
Ayla is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.