You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用microsoft-graph Java SDK单次调用跨多个邮件目录树搜索消息

解决方案

有两种实现方式,可根据你的搜索范围选择:

  • 场景1:仅需搜索常规邮箱目录(收件箱、用户自定义文件夹、垃圾邮件、已删除项等)
    直接调用用户的顶层messages接口即可,该接口默认会跨所有常规邮箱目录检索,无需指定具体文件夹、无需循环调用,Java SDK示例代码如下:
MessageCollectionPage mcp = GraphServiceClient
      .builder()
      .authenticationProvider(authenticationProvider)
      .buildClient()
      .users(user)
      .messages()
      .buildRequest()
      .filter("internetMessageId eq '" + msgId + "'")
      .get();

该实现的检索效果和你之前EWS中查询AllItems目录的效果完全一致。

  • 场景2:需要同时包含可恢复项删除目录(RecoverableItemsDeletions)
    RecoverableItemsDeletions属于系统隐藏目录,不会被顶层messages接口包含,这时候可以使用Graph的*批量请求($batch)*能力,把多个文件夹的查询打包成单次HTTP请求发送,服务端处理完成后会一次性返回所有结果,等效于单次调用覆盖多个目录,Java SDK示例代码如下:
GraphServiceClient client = GraphServiceClient
      .builder()
      .authenticationProvider(authenticationProvider)
      .buildClient();

// 构造批量请求容器
BatchRequestContent batchContent = new BatchRequestContent();
// 添加AllItems目录的查询请求
String allItemsReqId = batchContent.addGetRequest(
    client.users(user).mailFolders("AllItems").messages().buildRequest().filter("internetMessageId eq '" + msgId + "'")
);
// 添加RecoverableItemsDeletions目录的查询请求
String deletionsReqId = batchContent.addGetRequest(
    client.users(user).mailFolders("RecoverableItemsDeletions").messages().buildRequest().filter("internetMessageId eq '" + msgId + "'")
);

// 单次提交所有请求
BatchResponseContent batchResp = client.batch().buildRequest().post(batchContent);

// 分别解析两个目录的查询结果
MessageCollectionPage allItemsResult = batchResp.getResponseById(allItemsReqId, MessageCollectionPage.class);
MessageCollectionPage deletionsResult = batchResp.getResponseById(deletionsReqId, MessageCollectionPage.class);

该方式既减少了多次HTTP请求带来的网络开销,也完全匹配你需要的单次调用覆盖多个目录的需求,最终检索效果和EWS原有实现一致。


内容的提问来源于stack exchange,提问作者mobileAgent

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 13:06:03