'Why win32com doesnt show me all emails

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?



Solution 1:[1]

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 !

Solution 2:[2]

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.

Solution 3:[3]

Try to get the Count , Example

print(messages.Count)

also to check item type, try

if i.Class == 43:
        print(i.subject)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Manolis V
Solution 2 Eugene Astafiev
Solution 3 0m3r