pythonoutlookwin32comcom-automation

Find specific mail in Outlook by title


I am trying to create a code that will find letters in my mailbox using Subject field (according Outlook rules this letters will be located in folder 'TODO' for example). That's what I have for now:

import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

inbox = outlook.GetDefaultFolder(6).Folders.Item("TODO")

messages = inbox.Items
message = messages.GetLast()
body_content = message.subject
print(body_content)

This code finds the last letter at the folder.

Thank you in advance.


Solution

  • The below code will search in all the messages of TODO folder and if the subject matches with the String to be searched, it will print found message

    import win32com.client
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    
    inbox = outlook.GetDefaultFolder(6).Folders.Item("TODO")
    
    messages = inbox.Items
        for message in messages:
            if message.subject == 'String To be Searched':
                print("Found message")