Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outlook using python win32com to iterate subfolders

I have the following code which gets me the inbox of my shared folder, and all of the emails inside. This code works great and will print the subject of the last email.

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
recip = outlook.CreateRecipient("[email protected]")
inbox = outlook.GetSharedDefaultFolder(recip, 6)
messages = inbox.Items
message = messages.GetLast()
print (message.Subject)

I can access other parent folders in [email protected]'s mailbox (like Sent), but I can't get any subfolders of a folder within the inbox, or deeper than that. So if I want inbox\subfolder1, how do I access that? Using Outlook 2013 if that matters. My main goal is to do:

message.Move(inbox\subfolder1)
like image 612
nico Avatar asked Nov 28 '16 17:11

nico


2 Answers

Yeah its better to write it as the name of the folder instead of writing the folder numbers

Like my folder hierarchy is : Outlook_Mails > Inbox > Important

outlook = win32.com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace("MAPI")

your_folder = mapi.Folders['Outlook_Mails'].Folders['Inbox'].Folders['Important']
for message in your_folder.Items:
    print(message.Subject)
like image 93
Sir Tesla Avatar answered Oct 27 '22 00:10

Sir Tesla


This is the code I'm using to do a similar task.

outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")
root_folder = namespace.Folders.Item(1)
subfolder = root_folder.Folders['All'].Folders['Main Folder'].Folders['Subfolder']
messages = subfolder.Items

This finds the messages in the folder "All/Main Folder/Subfolder".

like image 39
Jared Goguen Avatar answered Oct 27 '22 01:10

Jared Goguen