【问题标题】:Filter messages from ALL folders in an account with exchangelib使用 exchangelib 过滤来自帐户中所有文件夹的邮件
【发布时间】:2019-01-22 18:03:20
【问题描述】:

我正在尝试使用 exchangelib 从与特定主题匹配\开头的帐户中检索消息。这必须包括来自所有文件夹的邮件,而不仅仅是“收件箱”文件夹。我在这里遇到了几个类似的问题

How can I get the latest emails from all folders with exchangelib?

这导致我使用 FolderCollection。但是,我不清楚如何创建所有文件夹的列表并将其传递给“文件夹”参数。

目前我尝试了以下方法:

conf = Configuration(server=ex_srv, credentials=cred)
acct = Account(primary_smtp_address=email, 
                            config=conf, 
                            autodiscover=False, 
                            credentials=cred, 
                            access_type=IMPERSONATION
                        )

from exchangelib.folders import Messages, FolderCollection
all_folder_list = acct.folders[Messages]
all_folder_collection = FolderCollection(account=acct, folders=all_folder_list)

all_folder_collection.filter(subject__startswith='test')

我似乎在这里犯了一个愚蠢的错误,但无法抓住它。

有人可以告诉我实现此用例的最佳方式吗?

编辑:

我看到的错误是

Traceback (most recent call last):

  File "queryset.py", line 298, in __iter__
    for val in self._format_items(items=self._query(), return_format=self.return_format):
  File "queryset.py", line 375, in _item_yielder
    for i in iterable:
  File "account.py", line 580, in fetch
    shape=ID_ONLY,
  File "account.py", line 302, in _consume_item_service
    is_empty, items = peek(items)
  File "util.py", line 118, in peek
    first = next(iterable)
  File "folders.py", line 250, in find_items
    for i in items:
  File "services.py", line 432, in _paged_call
    parsed_pages = [self._get_page(message) for message in response]
  File "services.py", line 432, in <listcomp>
    parsed_pages = [self._get_page(message) for message in response]
  File "services.py", line 481, in _get_page
    rootfolder = self._get_element_container(message=message, name='{%s}RootFolder' % MNS)
  File "services.py", line 345, in _get_element_container
    raise self._get_exception(code=response_code, text=msg_text, msg_xml=msg_xml)
exchangelib.errors.ErrorInvalidOperation: Shared folder search cannot be performed on multiple folders.

【问题讨论】:

  • 您看到的错误是什么?最新版本的 exchangelib 没有 Account.folders 字典,但您可以在 walk()glob() 文件夹中包含子文件夹。请参阅github.com/ecederstrand/exchangelib#folders 您甚至可以直接对其进行过滤,例如root.walk().filter(subject__startswith='test')FolderCollection 接受文件夹列表或生成器,因此您的代码看起来正确。
  • 埃里克,感谢您的回复。我得到的错误是“无法在多个文件夹上执行共享文件夹搜索”。我找不到足够的信息来说明为什么会发生这种情况,但鉴于我正在搜索一组文件夹,我对这个错误感到困惑。我假设我没有传入正确的文件夹列表。这是我使用 root.walk().filter()
  • 能否将完整的堆栈跟踪附加到原始帖子中?
  • 谢谢埃里克。我已经添加了堆栈跟踪。

标签: python exchangewebservices exchangelib


【解决方案1】:

问题在于您的 FolderCollection 包含多个共享文件夹。 EWS 不允许一次查询多个共享文件夹。如果您不需要搜索共享文件夹,则必须从搜索中排除共享文件夹,或者一次搜索一个共享文件夹。

【讨论】:

  • 感谢 Erik 提供的帮助。我花了一些时间来弄清楚如何过滤掉它。我用 account.root.walk() 替换了 all_folder_list = acct.folders[Messages] 并将每个文件夹添加到 all_folder_list。但我无法弄清楚文件夹上的哪个属性表明它是共享文件夹。我看到几个字段,例如 Effective_rights 等,但我不确定要使用哪个字段。或者,由于我知道我只想搜索电子邮件而不是查看其他类型,如便笺、联系人等,有没有办法最初过滤掉只包含消息的文件夹?
  • 我相信共享文件夹存储在account.public_folders_root 下,但我对共享文件夹没有太多经验。文件夹实例有一个supported_item_models 属性,说明它支持的项目类型。见:github.com/ecederstrand/exchangelib/blob/…
猜你喜欢
  • 2018-06-13
  • 2020-05-20
  • 2018-07-17
  • 2018-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
相关资源
最近更新 更多