JavaMail 连接到 Office 365 XOAUTH2 以进行 IMAP 身份验证失败

JavaMail connecting to Office 365 XOAUTH2 for IMAP Authentication fails

使用 OAuth2.0 在线时面临 Office365 的连接问题 我已经设置了应用程序权限,IMAP 和 SMTP connection.Basic 身份验证似乎工作正常。我相信 IMAP 已启用。我的应用程序配置为任何组织目录(任何 Azure AD 目录 - 多租户)中的帐户,并使用授权类型授权代码。

并添加了授权的 Microsoft Graph 范围 https://graph.microsoft.com/IMAP.AccessAsUser.All: Client scopes added

请求的授权码 https://login.microsoftonline.com/{tenantID}/oauth2/v2.0/authorize?response_type=code&client_id=1223&redirect_uri=http://localhost:5555 访问令牌请求 https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token 请求的访问令牌的资源为“https://graph.microsoft.com”

Requesting Access token Image

已成功收到范围为 IMAP.AccessAsUser.All SMTP.Send

的访问令牌
{
    "token_type": "Bearer",
    "scope": "IMAP.AccessAsUser.All SMTP.Send",
    "expires_in": 3599,
    "ext_expires_in": 3599,
    "access_token": "access_token",
    "refresh_token": "refresh_token",
    "id_token": "id_token"
} 

所以这是 Java 代码(Java使用 Mail jar 1.6.2)

Properties properties= new Properties();
properties.put("mail.imap.ssl.enable", "true");
properties.put("mail.imap.auth.mechanisms", "XOAUTH2");
//properties.put("mail.imap.sasl.enable", "true"); un-commented still results are same
properties.put("mail.imap.auth.login.disable", "true");
properties.put("mail.imap.auth.plain.disable", "true");
properties.put("mail.debug", "true");
properties.put("mail.debug.auth", "true");

Session session = Session.getInstance(props);
session.setDebug(true);

String userEmail = "emailuser@domain.onmicrosoft.com";
String accessToken = "accessToken";

final Store store = session.getStore("imap");
store.connect("outlook.office365.com","993",userEmail, accessToken);

以下输出:

DEBUG: JavaMail version 1.6.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: closeFoldersOnStoreFailure
DEBUG IMAP: trying to connect to host "outlook.office365.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready. [TQBB]
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE 
NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: AUTH: XOAUTH2
DEBUG IMAP: protocolConnect login, host=outlook.office365.com, user=emailuser@domain.onmicrosoft.com, 
password=<non-null>
A1 AUTHENTICATE XOAUTH2 dXNlAQE=
A1 NO AUTHENTICATE failed.
Could not connect to the message store
javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:731)
at javax.mail.Service.connect(Service.java:366)
at myproject.EmailReceiver.downloadEmails(EmailReceiver.java:79)
at myproject.EmailReceiver.main(EmailReceiver.java:179)

关注其他帖子无法在我的 Azure 中找到范围 https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/SMTP.Send。可能是遗留范围。

除了“https://graph.microsoft.com/IMAP.AccessAsUser.All”和“https://graph.microsoft.[=43 之外还有其他范围吗? =].send" 需要通过 IMAP 在线连接到 Exchange。或者现有代码有任何问题。

问题已解决,我使用范围 offline_access%20https%3A%2F%2Foutlook。office365.com%2FIMAP.AccessAsUser.All%20https%3A%2F%2Foutlook。office365.com%2FSMTP.Send 它提供访问邮件的权限,还提供刷新令牌以重新生成访问令牌。

注意: 将范围更改为 offline_access https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/SMTP.Send 解决了问题。

我认为您的问题与访问令牌范围有关。我正在开发一个类似的应用程序,但我正在使用范围 https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/SMTP.Send 来获取访问令牌,如果我尝试使用 SMTP.Send 和 IMAP.AccessAsUserAll.

似乎不起作用

我的访问令牌如下所示: {"token_type":"承载", “范围”:“https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/SMTP.Send", “expires_in”:3599, “ext_expires_in”:3599, "access_token":"eyJ0eXAiOi..."}

在 Azure 应用程序配置中,我只有 IMAP 和 SMTP 的 Microsoft Graph 权限。