Reputation: 91
I want to parse emails in python through the Outlook application. Running this code I get only a few of my emails.
import win32com.client
outlook = win32com.client.Dispatch('outlook.application')
mapi = outlook.GetNamespace("MAPI")
inbox= mapi.GetDefaultFolder(6)
messages= inbox.Items
for i in messages:
message=i.subject
print(message)
I have tried changing the Default Folder and it happens everywhere. What have I done wrong?
Upvotes: 4
Views: 1780
Reputation: 91
Emails were not visible beacuse they were on the server!
File-> Account Settings-> Account Settings...-> double click on your Exchange account-> set the Mail to keep offline slider to: All.
Found it here !
Upvotes: 3
Reputation: 12499
Try to get the Count
, Example
print(messages.Count)
also to check item type, try
if i.Class == 43:
print(i.subject)
Upvotes: 0
Reputation: 49397
You need to remember that Outlook folders may contains different kind of items in the folder. So, you must be prepared for different items with different set of properties located in the same folder. For that reason I'd recommend checking the item type and only then try to get any property or call methods. Following that way you could process all items in the folder. Otherwise, when you call non-existing property or method an exception could be thrown in the loop and it will end its works suddenly.
You can use the MessageClass
property of Outlook items for checking the item type. See Item Types and Message Classes for more information.
Upvotes: 2