Search code examples
pythonoutlookautomation

How to Get 'Recipient' Names and emails address from Outlook using Python MAPI


Just like .SenderName gives me names of the Senders which can be easily appended to a list, is their any object to do the same for the 'Recipients' of my mails.

I have tried .Recipients but once added to the list they appear as COMObjects which cannot be manipulated by Python.

All I want is a simple list of Recipient names.


Solution

  • The simplest way to do that is

    Example

    import win32com.client
    
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    inbox = outlook.Folders["[email protected]"].Folders["Inbox"]
    
    
    def get_email_address():
        for message in inbox.Items:
            print("========")
            print("Subj: " + message.Subject)
            print("Email Type: ", message.SenderEmailType)
            if message.Class == 43:
                try:
                    if message.SenderEmailType == "SMTP":
                        print("Name: ", message.SenderName)
                        print("Email Address: ", message.SenderEmailAddress)
                        print("Date: ", message.ReceivedTime)
                    elif message.SenderEmailType == "EX":
                        print("Name: ", message.SenderName)
                        print("Email Address: ", message.Sender.GetExchangeUser(
                                                                  ).PrimarySmtpAddress)
                        print("Date: ", message.ReceivedTime)
                except Exception as e:
                    print(e)
                    continue
    
    
    if __name__ == '__main__':
        get_email_address()