简体   繁体   中英

Read outlook Email from one of my 2 email addresses using win32 python

I have 2 outlook email accounts opened in app. So trying to read the emails of one particular account using python. I have tried few steps but didn't work. Any suggestions on how can I do that. I know how to read emails if i have only one account but not sure how to do that with 2.

code below:
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
wich_accnt = outlook.Folders
#try the restrict method!
for i in wich_accnt:
    if (i.Name == 'autoenquirytest@robo.com'):
        outbox = outlook.GetDefaultFolder(6) 
        messages = outbox.Items
        print(messages[1].SenderName)
The code passes through if condition, but then how can we read with that particular emails inbox.

outbox = outlook.GetDefaultFolder(6) 
messages = outbox.Items
print(messages[1].SenderName)

when I run the code for getting inbox 'outbox = outlook.GetDefaultFolder(6)' 
I'm getting this error = 
AttributeError: '<win32com.gen_py.Microsoft Outlook 16.0 Object Library.MAPIFolder instance at 0x1560956608624>' object has no attribute 'GetDefaultFolder'

Have fixed the issue by finding the folder with the name 'Inbox' in subfolder instead of using index.

Here is the code

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
wich_accnt = outlook.Folders
#try the restrict method!
for i in (range(len(outlook.Folders))):
    if (outlook.Folders.Item(i+1).Name == 'autoenquirytest@robo.com'):
        root_folder = outlook.Folders.Item(i+1)
        for folder in root_folder.Folders:
            if (folder.Name == 'Inbox'):
                messages = folder.Items
                messages.Sort("[ReceivedTime]", True) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM