'Extract Recipients Email Address in Outlook using Python Win32com
I'm trying to extract the Recipient email address in Python using Win32com client.
Here's my code so far:
import win32com.client
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders["[my email address"].Folders["Inbox"]
def get_email_address():
for message in inbox.Items:
print("========")
print("Subj: " + message.Subject)
print('To:', message.Recipients) #this part does not work
print("Email Type: ", message.SenderEmailType)
if message.Class == 43:
try:
if message.SenderEmailType == "SMTP":
print("Name: ", message.SenderName)
print("Email Address: ", message.SenderEmailAddress)
print('To:', message.Recipients) #this part does not work
print("Date: ", message.ReceivedTime)
elif message.SenderEmailType == "EX":
print("Name: ", message.SenderName)
print("Email Address: ", message.Sender.GetExchangeUser(
).PrimarySmtpAddress)
print('To:', message.Recipients) #this part does not work
print("Date: ", message.ReceivedTime)
except Exception as e:
print(e)
continue
if __name__ == '__main__':
get_email_address()
As you can see, I can get the sender email address...but how do I get the recipient email address?
Solution 1:[1]
It is similar to what you do with the sender - loop through recipients in the MailItem.Recipients
collection and for each Recipient
use the Recipient.AddressEntry
property to do what you are already doing with the MailItem.Sender
property.
Also note that this is not the most efficient way - opening an address entry can be expensive or outright impossible if the profile does not have the parent Exchange server, e.g. if you are processing a standalone MSG file or a message copied to a PST from an Exchange mailbox. In most cases the SMTP addresses are available on the message directly, e.g. from the PidTagSenderSmtpAddress
(DASL name http://schemas.microsoft.com/mapi/proptag/0x5D01001F
) which can be accessed using MailItem.PropertyAccessor.GetProperty
. Similarly, recipient SMTP address might be available in the PR_SMTP_ADDRESS
property (DASL name http://schemas.microsoft.com/mapi/proptag/0x39FE001F
, use Recipient.PropertyAccessor.GetProperty
) - you can see these properties in OutlookSpy (I am its author - click IMessage button property).
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 |