简体   繁体   中英

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.

The simplest way to do that is

Example

import win32com.client

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders["0m3r@Email.com"].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()

The MailItem.Recipients property returns a Recipients collection that represents all the recipients for the Outlook item. In VBA, for example, you can use the following:

Dim recip As Recipient
Dim allRecips As String

For Each recip In item.Recipients
    If (Len(allRecips) > 0) Then allRecips = allRecips & "; "
    allRecips = allRecips & recip.Address
Next

The Outlook object model is common for all kinds of programming languages and applications. So, you can use the same property and method calls to retrieve the same results.

The Type property of the Recipient class returns or sets an integer representing the type of recipient. For the MailItem the value can be one of the following OlMailRecipientType constants: olBCC, olCC, olOriginator, or olTo.

Also the Recipient class provides the following properties:

  • Name - a string value that represents the display name for the recipient.
  • Address - a string representing the e-mail address of the Recipient.
  • AddressEntry - the AddressEntry object corresponding to the recipient.

The sample shared by Eugene Astafiev may have worked at some point of time, but nowadays it doesnt. The parameter ".Address" when you are connected to Exchange returns you a weird string now instead of the email:

"/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (FYDIBOHF69SPDLT)/CN=RECIPIENTS/CN=537B0812B1FC48F78FBBEE417EC83959-YOUR NAME HERE"

The .Address property only returns email addresses for non-Exchange accounts. For Exchange accounts, it returns that weird string. To extract email addresses only for both Exchange and non-exchange accounts:

for x in message.Recipients:
     try:
         print(x.AddressEntry.GetExchangeUser().PrimarySmtpAddress)
     except AttributeError:
         print(x.AddressEntry.Address)

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