3

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Lin Chen
  • 71
  • 2
  • 6

1 Answers1

0

Found on here

Note

If you've enabled security defaults in your organization, POP3 and IMAP4 are automatically disabled in Exchange Online. For more information, see What are security defaults?.

To protect your Exchange Online tenant from brute force or password spray attacks, your organization will need to Disable Basic authentication in Exchange Online and only use Modern authentication in Exchange Online. Disabling Basic authentication will block legacy protocols, such as POP and IMAP.

Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
vsj
  • 1
  • 1
    According to https://stackoverflow.com/questions/29747477/imap-auth-in-office-365-using-oauth2, Microsoft is still actively working on OAuth support for IMAP connections to O365 mailboxes. Is it right? – Lin Chen Dec 11 '19 at 13:22