I can use the Javamail IMAP package to access my outlook.office365.com mailbox. I want to access the same mailbox by using OAuth2.0. According to https://javaee.github.io/javamail/OAuth2, after 1.5.5 JavaMail is able to support OAuth2 by using the Bearer access token. I created a simple testing code:
System.out.println("Helloworld");
String host="outlook.office365.com";
String username="mymailboxname";
//String password="mymailboxpasswd";
String accesstoken="eyJ0eXAiOiJKV1QiLCJub25jZSI6Im4....my token from Micorsoft .....S0QoWgvodHXw";
Properties props=new Properties();
props.setProperty("mail.imap.ssl.enable","true");
props.setProperty("mail.imap.auth.mechanisms","XOAUTH2"); //added for oauth2
// set any other needed mail.imap.* properties here
Session session=Session.getInstance(props);
Store store=session.getStore("imap");
// store.connect(host,993,username,password);
store.connect(host,993,username,accesstoken);
When I ran it, I got AuthenticationFailedException
Exception in thread "main" javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:732)
at javax.mail.Service.connect(Service.java:366)
at Main.main(Main.java:21)
What is the way to connect to outlook.office365.com with IMAP by using OAuth2