尝试使用Python和Exchangelib删除收件箱外部文件夹中的多个电子邮件

时间:2019-01-25 20:39:47

标签: python python-3.x exchangelib

我正在尝试使用Python 3和exchangelib删除account.inbox文件夹之外的文件夹中的所有电子邮件。

testFolder = account.root / 'Top of Information Store' / 'Test'    
emails = testFolder.all().order_by('-datetime_received')
for email in emails:
    email.delete()
    # FWIW
    #print(email.subject)
    #email.move(to_another_folder)
    #both work fine.

以上内容仅显示所有电子邮件及其属性

我也尝试过:

items=[]
testFolder = account.root / 'Top of Information Store' / 'Test'    
emails = testFolder.all().order_by('-datetime_received')
for email in emails:
    items.append(Message(folder=testFolder))
items.save()
items.delete()

以上内容挂起并给出内存错误:

File "C:\Python36-32\lib\site-packages\exchangelib\services.py", line 89, in _
get_elements
    response = self._get_response_xml(payload=payload)
  File "C:\Python36-32\lib\site-packages\exchangelib\services.py", line 171, in
_get_response_xml
    res = self._get_soap_payload(response=r, **parse_opts)
  File "C:\Python36-32\lib\site-packages\exchangelib\services.py", line 260, in
_get_soap_payload
    root = to_xml(response.iter_content())
  File "C:\Python36-32\lib\site-packages\exchangelib\util.py", line 365, in to_x
ml
    return parse(stream, parser=forgiving_parser)
  File "C:\Python36-32\lib\site-packages\defusedxml\lxml.py", line 134, in parse
  elementtree = _etree.parse(source, parser, base_url=base_url)
  File "src\lxml\etree.pyx", line 3424, in lxml.etree.parse
  File "src\lxml\parser.pxi", line 1857, in lxml.etree._parseDocument
  File "C:\Python36-32\lib\site-packages\exchangelib\util.py", line 340, in getv
alue
    res = b''.join(self._bytes_generator)
MemoryError

我不确定我还应该尝试什么,特别是因为前者可以很好地移动和打印电子邮件。

2 个答案:

答案 0 :(得分:1)

无需先收集物品。如您所知,这将消耗大量内存。相反,您可以直接在QuerySet上调用.delete()。这样只会获取商品ID,并批量删除它们。

还有一个.empty()方法会导致对EmptyFolder服务的调用。这将删除文件夹中的所有项目,甚至不获取ID:

testFolder.all().delete()
# Or even faster:
testFolder.empty()

答案 1 :(得分:0)

也许不是删除而是尝试移至垃圾桶?还是软删除?