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

99封情书 提交于 2020-01-04 16:56:54

问题


Currently, I use

latest_mails = account.inbox.filter(datetime_received__gt=emails_since)

But it seems to miss received emails which are in subfolders.

Printing all folders with

for f in account.root.get_folders():
    print(f)

gives something like

Calendar (Kalender)
Contacts (Kontakte)
Contacts (Vorgeschlagene Kontakte)
Folder (AllItems)
Folder (Calendar Logging)
Folder (Common Views)
Folder (Conversation Action Settings)
Folder (Deferred Action)
Folder (Deletions)
Folder (Erinnerungen)
Folder (ExchangeSyncData)
Folder (Finder)
Folder (Infected Items)
Folder (Journal)
Folder (Location)
Folder (MailboxAssociations)
Folder (Notizen)
Folder (Recipient Cache)
Folder (Recoverable Items)
Folder (Schedule)
Folder (Shortcuts)
Folder (Spooler Queue)
Folder (System)
Folder (Versions)
Folder (Views)
Folder (WorkingSet)
Messages (Postausgang)
Messages (Posteingang)
Messages (foo)
Messages (bar)
Messages (something is)
Messages (here)
Messages (Gelöschte Elemente)
Messages (Gesendete Elemente)
Messages (Junk-E-Mail)
Messages (Meine Kontakte)
Messages (MyContactsExtended)
Messages (Nachverfolgte E-Mail-Verarbeitung)
Messages (Zugang)
Tasks (Aufgaben)
Tasks (Aufgabensuche)

So I only want to look at the "Messages" folders, but at all of them. Is that possible (without using account.root.get_folders()) and looping over the results (which took about 5 minutes)


回答1:


You're correct that .filter() only works on the folder you call it on, not subfolders. I'm pretty sure EWS only supports searching one folder at a time.

You should be able to do something like this to speed things up a little bit:

from exchangelib.folders import Messages

for f in account.folders[Message]:
    for i in f.filter(datetime_received__gt=emails_since):
        print(i)

But Folder type folders can also contain Message items, so depending on your needs you may also have to visit those.

f.supported_item_models will tell you which item types a given folder can contain.



来源:https://stackoverflow.com/questions/45935701/how-can-i-get-the-latest-emails-from-all-folders-with-exchangelib

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!