问题
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