'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 1:[1]
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.
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eugene Astafiev |
| Solution 2 | Niranjana Shashikumar |
