Hi Kinjal1012
I've included a snippet of my code that is working successfully. When using OAuth, you shouldn't be adding an Authenticator to the Session variable (instead leave it as null) since you have already obtained the access (bearer) token. That token should be added as a value to the "mail.smtp.sasl.mechanisms.oauth2.oauthToken" property as shown below. Also include that token as a parameter in the connect method of the transport.
// Create a mail session
Properties props = new Properties();
props.put("mail.smtp.connectiontimeout", "10000");
props.put("mail.smtp.timeout", "10000");
props.put("mail.smtp.sendpartial", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", "*");
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", "" + smtpPort);
props.put("mail.smtp.auth", "true");
// Get the bearer token for OAUTH2 Access to the MS Exchange/365 server
OAuthToken oauth = new OAuthToken();
String authToken = oauth.getAuthToken();
props.put("mail.smtp.auth.mechanisms", "XOAUTH2");
props.put("mail.smtp.sasl.mechanisms.oauth2.oauthToken", authToken);
props.put("mail.smtp.auth.login.disable", "true");
props.put("mail.smtp.auth.plain.disable", "true");
props.put("mail.smtp.auth.xoauth2.disable", "false");
if (debug) {
props.put("mail.debug.auth", "true");
props.put("mail.debug.auth.password", "true");
}
Session session = Session.getInstance(props, null);
session.setDebug(debug);
// Construct the message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(fromAddr));
msg.setSubject(subject);
msg.setText(content);
msg.saveChanges();
try {
transport = session.getTransport("smtp");
transport.connect(user, authToken);
transport.sendMessage(msg, msg.getAllRecipients());
} finally {
transport.close();
}
Hope this helps!