I am trying to fetch the inbox from my personal email which is @outlook.com. I have created Azure account and created application and as well as secret as well. But I am getting error
A1 NO AUTHENTICATE failed
. I have account type Accounts in any organizational directory (Any Microsoft Entra ID tenant – Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox). But I have tried using other options as well.
Below is the code.
<code> private static String generateAccessToken(String clientId, String clientSecret, String tenantId)
throws MalformedURLException, ExecutionException, InterruptedException {
ConfidentialClientApplication
app = ConfidentialClientApplication.builder(clientId, ClientCredentialFactory.createFromSecret(clientSecret))
.authority(authUrl+"/"+tenantId)
.build();
ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(Collections.singleton(scope)).build();
CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
boolean isAuthorized = app.validateAuthority();
System.out.println("isAuthorized = " + isAuthorized);
System.out.println("url = " + app.authority());
IAuthenticationResult result = future.get();
return result.accessToken();
}
private static Store getIMAPStore() throws NoSuchProviderException {
Properties props = getMailIMAPProperties();
Session session = Session.getInstance(props);
session.setDebug(true);
return session.getStore("imaps");
}
private static Properties getMailIMAPProperties() {
Properties props = new Properties();
props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
props.put("mail.imaps.auth.plain.disable", "true");
props.put("mail.imaps.auth.xoauth2.disable", "false");
return props;
}
public static void main(String[] args) throws MalformedURLException, ExecutionException, InterruptedException {
String host = "outlook.office365.com";
String email = "[email protected]";
String clientId = "client_id";
String clientSecret = "clientSecret";
String tenantId = "tenantId";
String accessToken = generateAccessToken(clientId, clientSecret, tenantId);
try{
Store store = getIMAPStore();
store.connect(host, email, accessToken);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
System.out.printf("Total messages present in the folder 'Inbox' are %d.%n", messageCount);
Message[] messages = inbox.getMessages();
for (Message message : messages) {
System.out.println(message.getSubject());
}
inbox.close(false);
store.close();
}catch (Exception e){
e.printStackTrace();
}
}
</code>
<code> private static String generateAccessToken(String clientId, String clientSecret, String tenantId)
throws MalformedURLException, ExecutionException, InterruptedException {
ConfidentialClientApplication
app = ConfidentialClientApplication.builder(clientId, ClientCredentialFactory.createFromSecret(clientSecret))
.authority(authUrl+"/"+tenantId)
.build();
ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(Collections.singleton(scope)).build();
CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
boolean isAuthorized = app.validateAuthority();
System.out.println("isAuthorized = " + isAuthorized);
System.out.println("url = " + app.authority());
IAuthenticationResult result = future.get();
return result.accessToken();
}
private static Store getIMAPStore() throws NoSuchProviderException {
Properties props = getMailIMAPProperties();
Session session = Session.getInstance(props);
session.setDebug(true);
return session.getStore("imaps");
}
private static Properties getMailIMAPProperties() {
Properties props = new Properties();
props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
props.put("mail.imaps.auth.plain.disable", "true");
props.put("mail.imaps.auth.xoauth2.disable", "false");
return props;
}
public static void main(String[] args) throws MalformedURLException, ExecutionException, InterruptedException {
String host = "outlook.office365.com";
String email = "[email protected]";
String clientId = "client_id";
String clientSecret = "clientSecret";
String tenantId = "tenantId";
String accessToken = generateAccessToken(clientId, clientSecret, tenantId);
try{
Store store = getIMAPStore();
store.connect(host, email, accessToken);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
System.out.printf("Total messages present in the folder 'Inbox' are %d.%n", messageCount);
Message[] messages = inbox.getMessages();
for (Message message : messages) {
System.out.println(message.getSubject());
}
inbox.close(false);
store.close();
}catch (Exception e){
e.printStackTrace();
}
}
</code>
private static String generateAccessToken(String clientId, String clientSecret, String tenantId)
throws MalformedURLException, ExecutionException, InterruptedException {
ConfidentialClientApplication
app = ConfidentialClientApplication.builder(clientId, ClientCredentialFactory.createFromSecret(clientSecret))
.authority(authUrl+"/"+tenantId)
.build();
ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(Collections.singleton(scope)).build();
CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
boolean isAuthorized = app.validateAuthority();
System.out.println("isAuthorized = " + isAuthorized);
System.out.println("url = " + app.authority());
IAuthenticationResult result = future.get();
return result.accessToken();
}
private static Store getIMAPStore() throws NoSuchProviderException {
Properties props = getMailIMAPProperties();
Session session = Session.getInstance(props);
session.setDebug(true);
return session.getStore("imaps");
}
private static Properties getMailIMAPProperties() {
Properties props = new Properties();
props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
props.put("mail.imaps.auth.plain.disable", "true");
props.put("mail.imaps.auth.xoauth2.disable", "false");
return props;
}
public static void main(String[] args) throws MalformedURLException, ExecutionException, InterruptedException {
String host = "outlook.office365.com";
String email = "[email protected]";
String clientId = "client_id";
String clientSecret = "clientSecret";
String tenantId = "tenantId";
String accessToken = generateAccessToken(clientId, clientSecret, tenantId);
try{
Store store = getIMAPStore();
store.connect(host, email, accessToken);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
int messageCount = inbox.getMessageCount();
System.out.printf("Total messages present in the folder 'Inbox' are %d.%n", messageCount);
Message[] messages = inbox.getMessages();
for (Message message : messages) {
System.out.println(message.getSubject());
}
inbox.close(false);
store.close();
}catch (Exception e){
e.printStackTrace();
}
}
Below is the some of the setup of azure.