I am building a java micro-service to read mails.

I am trying to connect to com.sun.mail:javax-mail:1.5.5 imap using google's OAuth token, I am sending access_token in place of password and its throwing Authentication error.

[AUTHENTICATIONFAILED] Invalid credentials (Failure)

Properties props = new Properties();
props.put("mail.imaps.ssl.enable", "true");
props.put("mail.imaps.auth.mechanisms", "XOAUTH2");

 Session session = Session.getInstance(props);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", 993, "[email protected]", accessToken);

0 Answers