使用exchangelib

时间:2019-01-15 17:06:30

标签: python exchangewebservices exchangelib

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

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

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

目前我尝试了以下方法:

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.

1 个答案:

答案 0 :(得分:0)

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