- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前一直在尝试通过 IMAP 协议(protocol)访问 Office 365 帐户。我正在使用 Jakarta Mail (Javax.mail) 来尝试连接。当我尝试进行身份验证时,出现以下错误:
DEBUG IMAP: trying to connect to host "imap-mail.outlook.com", port 993, isSSL true
* OK The Microsoft Exchange IMAP4 service is ready. [...]
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=imap-mail.outlook.com, user=[...], password=<non-null>
DEBUG IMAP: SASL Mechanisms:
DEBUG IMAP: XOAUTH2
DEBUG IMAP:
DEBUG IMAP: SASL client XOAUTH2
DEBUG IMAP: SASL callback length: 2
DEBUG IMAP: SASL callback 0: javax.security.auth.callback.NameCallback@11d86b9d
DEBUG IMAP: SASL callback 1: javax.security.auth.callback.PasswordCallback@6dce59e
A1 AUTHENTICATE XOAUTH2 [...]
A1 NO AUTHENTICATE failed.
对于我正在制作的系统,我不必每次都从登录链接手动获取访问 token 。就这一点而言,我认为最明智的举动是使用 grant_type=client_credentials
。但是,我还没有让它发挥作用。
我用于连接的代码如下:
Properties props = new Properties();
// SMTP
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
// IMAP
props.put("mail.imap.ssl.enable", "true"); // required for Gmail
props.put("mail.imap.sasl.enable", "true");
props.put("mail.imap.sasl.mechanisms", "XOAUTH2");
props.put("mail.imap.auth.login.disable", "true");
props.put("mail.imap.auth.plain.disable", "true");
// Debug
props.put("mail.debug", "true");
props.put("mail.debug.auth", "true");
Session session = Session.getInstance(props);
session.setDebug(true);
String accessToken = renewToken();
try {
Store mailStore = session.getStore("imap");
mailStore.connect("imap-mail.outlook.com", System.getenv("MAIL_USERNAME"), accessToken);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
并且 renewToken()
如下所示:
@Nullable
private String renewToken() {
if(System.currentTimeMillis() > tokenExpires) {
try {
String request = "client_id=" + URLEncoder.encode(System.getenv("CLIENT_ID"), StandardCharsets.UTF_8)
+ "&client_secret=" + URLEncoder.encode(System.getenv("CLIENT_SECRET"), StandardCharsets.UTF_8)
+ "&scope=" + URLEncoder.encode("https://outlook.office365.com/.default", StandardCharsets.UTF_8)
+ "&grant_type=client_credentials";
HttpURLConnection conn = (HttpURLConnection) new URL(TOKEN_URL).openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(request);
out.flush();
out.close();
conn.connect();
HashMap<String,Object> result = new ObjectMapper().readValue(conn.getInputStream(), new TypeReference<>() {});
tokenExpires = System.currentTimeMillis()+(((Number)result.get("expires_in")).intValue()* 1000L);
return (String)result.get("access_token");
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
}
return null;
}
最后,CLIENT_AUTHORITY 定义为 https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
其中 {tenant}
替换为 https://portal.azure.com 中的租户 ID
现在,我不太确定为什么会收到错误,也不太明白“NO AUTHENTICATE failed”错误的含义。唯一想到的是访问 token 要么生成错误,要么我没有请求正确的权限。
还应该提到的是,我尝试过使用主机“outlook.office365.com”而不是“imap-mail.outlook.com”,但没有任何运气。
最佳答案
按照本指南,我得到了它的工作:
Authentication Failure for IMAP using Client Credential flow for OAuth2.0 | Java | Exchange Online
对我来说,错误是 https://learn.microsoft.com 上提供的指南网站没有解释如何使用正确启用OAuth2.0所需的命令。
关于java - 使用 OAUTH2 服务器端的 IMAP 连接到 Outlook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73392053/
我有一堆代码执行 IMAP 命令“search, idle, done, search, idle, done, search, ...”。 是否有可能某些消息在搜索和空闲命令之间到达,因此只能在空闲
(使用IMAP命令,而不借助任何其他邮件程序包) 最佳答案 我不确定您对imap的了解程度如何,但是基本上在登录后,“选择”源邮箱,“复制”邮件,然后“删除”邮件(如果是,则“删除”旧邮箱)现在是空的
我正在编写一个 IMAP 爬虫,它是开源应用程序的一部分,对于增量爬网,我想使用消息 UID 来确定我是否已经看到了特定消息。 现在我找到了 a post从 2007 年开始说 IMAP UID 并不
我注意到 libcurl 支持 IMAP,但在文档和示例中没有找到任何内容 http://curl.haxx.se/libcurl/c/imap.html很穷。有谁知道如何在这个库中使用这个协议(pr
IMAP UID 是否保证为数字?我读过 RFC3501 中的部分,它说: Unique identifiers are assigned in a strictly ascending fashio
IMAP UID 是否保证为数字?我读过 RFC3501 中的部分,它说: Unique identifiers are assigned in a strictly ascending fashio
我正在使用开源 IMAP C# 库 IMapX (http://hellowebapps.com/products/imapx/)。当我试图从收件箱中获取电子邮件时,需要花费很多时间。有没有办法根据接
我正在尝试使用 Limilabs imap 库连接到电子邮件; tcpc = new System.Net.Sockets.TcpClient("imap.gmail.com", 993); 工作
我正在尝试使用 imap 方法通过 php 打开电子邮件,但它给我一个错误无法打开流 {imap.gmail.com:993/imap/ssl}INBOX,这是我的代码 $hostname = '{i
有没有办法确定给定电子邮件地址的 POP 或 IMAP 服务器?我正在为非技术用户构建一个应用程序,我真的不想打扰他们询问他们的 IMAP/POP 服务器。 mail2web.com这样做,但我不确定
如何确定通过 APPEND 添加的消息的 UID到邮箱?通过 STATUS我可以事先得到下一个值的预测,我可以 SEARCH之后,但依赖这些会引入竞争条件,因为可能在这些命令之间添加了其他消息。 最佳
这个搜索查询对我来说似乎有效: UID SEARCH OR ( OR ( OR (FROM "def@gmail.com") (FROM "abc@gmail.com")) (FROM "ghi@gm
我正在使用以下代码以编程方式验证电子邮件。 Properties properties = new Properties(); Session emailSession = Session.getDe
我正在使用 OpenSSL 连接到邮件服务器。 POP3 工作正常,但 IMAP 有问题。基于 CAPABILITY 命令服务器支持 PLAIN、NTLM 和 GSS-API 认证方法。 我想使用 P
我正在使用以下代码建立 IMAP 连接。我想阅读电子邮件。我读了这个文件 link无法从这里继续。 我的代码: #lang racket (define imap-server "*****") (d
在尝试使用 php 的 imap 函数时,我无法连接到 Gmail 的 imap 服务器。我正在使用: $mail = imap_open('{imap.gmail.com:993/imap/ssl/
我试图更好地理解 Gmail API . stated benefits 之一Gmail API 的最大特点是它可以“提供比 IMAP 显着的性能改进”。 Gmail API 和协议(protocol
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我正在制作一个支持 POP3 和 IMAP 的邮件客户端。我正在使用 vmime api 用 C++ 对客户端进行编程。我的问题是:同步邮件的最佳方式是什么? 我认为标识符是个好方法,但我不知道同步邮
我正在制作一个支持 POP3 和 IMAP 的邮件客户端。我正在使用 vmime api 用 C++ 对客户端进行编程。我的问题是:同步邮件的最佳方式是什么? 我认为标识符是个好方法,但我不知道同步邮
我是一名优秀的程序员,十分优秀!