如何使用Exchangelib python获取消息的父文件夹名称

问题描述

我有一个通过在 python 3.7 中使用 exchangelib 过滤帐户获得的 Item 对象。它是一个电子邮件对象。我需要找到这个项目的父文件夹名称。我特别需要文件夹的 name 字段(用于跟踪特定电子邮件在邮箱中的移动位置)。

我可以在 item 对象中看到一个字段 parent_folder_id,它返回我认为有效的文件夹 ID。这也适用于生产邮箱,其中 account.root.get_folder(folder_id=idObj) 由于我无法更改的 Exchange 设置而超时。几乎所有缓存请求都会因超时而失败。

account=Account(...)
mailItems=account.inbox.all().filter(subject="foo")
print([i.parent_folder_id.id for i in mailItems])

这将打印文件夹 ID 列表。我需要这些文件夹的名称。不清楚如何进行。任何帮助将不胜感激

解决方法

由于您只搜索 account.inbox 而不是其子文件夹,parent_folder_id 将始终指向 account.inbox

还没有一个很好的 API 来查找文件夹,例如ID。目前最好的解决方案是使用文件夹 QuerySet:

from exchangelib.folders import SingleFolderQuerySet

f = SingleFolderQuerySet(
    account=account,folder=account.root
).get(id=i.parent_folder_id.id)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...