I'm trying to connect to gmail server via javamail and trying to authenticate via OAuth.
Here is the code which does that.
public static void connect() {
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
props.put("mail.imaps.ssl.enable", "true"); //required for Gmail
props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
Session session = Session.getInstance(props);
Store store = session.getStore();
store.connect("imap.gmail.com", 993, "abcimap@gmail.com", "ya29.Glx2BW9zm7wSsr9WV66KhC4kZa7dbrOA9P6HT3EMwmiLbmkdjbHZM5oHi8VfHhxM-VNDntRxQBZ_GzMM2rMa1cAxnQ3GiNaR_M9SRfT9sCIXe0l4Rz_mNM8a40aqZw");
Folder folder = store.getFolder("Inbox");
IMAPFolder imapFolder = (IMAPFolder)folder;
}
But I get,
Exception in thread "main" javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Invalid credentials (Failure) at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:715) at javax.mail.Service.connect(Service.java:366)
A related question here : Problems with JavaMail, GMail and OAUTH2 (not Android)
asks to set scope. But I'm not sure where to set it. Whether it is in the java code or outside it.
Can you please help me fix this ?