Need example showing OAUTH2 connection to Microsoft Exchange in JavaMail

Viewed 543

I am trying to write a basic program in Java, using JavaMail, to send an email using Microsoft's new OAUTH2 mechanism.

On StackOverflow, I found the following code via a link, but it does not authenticate given a current access token:

public static void main(String[] args) throws IOException, MessagingException
{
    Properties props = new Properties();
    props.put("mail.imap.ssl.enable", "true"); 
    props.put("mail.imap.auth.mechanisms", "XOAUTH2");
    Session session = Session.getInstance(props);
    Store store = session.getStore("imap");
    String userEmail = "email_account_here";
    String accessToken = access_token_here"
    store.connect("imap-mail.outlook.com",993,userEmail, accessToken);
    session.setDebug(true);      
}

I am generating the token using a call to the Microsoft Graph API (not using JavaMail).

Result is "Authentication Failed".

I have searched but cannot find an example that works.

Would appreciate any code samples that work. :)

Thanks!

0 Answers
Related