Vin*_*und 8 imap jakarta-mail azure oauth-2.0 office365
面对 OAuth2.0 在线 Office365 的连接问题,我已经设置了应用程序权限以及 IMAP 和 SMTP 连接。基本身份验证似乎工作正常。我相信 IMAP 已启用。我的应用程序配置为任何组织目录(任何 Azure AD 目录 - 多租户)中的帐户,并使用授予类型授权代码。
并添加了 委派的 Microsoft Graph 范围https://graph.microsoft.com/IMAP.AccessAsUser.All :添加了客户端范围
使用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”
已成功接收范围为 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"
}
Run Code Online (Sandbox Code Playgroud)
这是Java代码(使用JavaMail 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);
Run Code Online (Sandbox Code Playgroud)
以下输出:
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)
Run Code Online (Sandbox Code Playgroud)
以下其他帖子无法在我的 Azure 中找到范围https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/SMTP.Send。可能它们是遗留范围。
除了“https://graph.microsoft.com/IMAP.AccessAsUser.All”和“https://graph.microsoft.com/SMTP.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 解决了该问题。
小智 2
我认为您的问题与访问令牌范围有关。我正在开发类似的应用程序,但我正在使用范围https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/SMTP.Send来获取访问令牌,但似乎没有如果我尝试使用 SMTP.Send 和 IMAP.AccessAsUserAll,则可以工作。
我的访问令牌如下所示:{“token_type”:“Bearer”,“scope”:“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权限。

| 归档时间: |
|
| 查看次数: |
8564 次 |
| 最近记录: |