I am trying to get the sender email address of MailItem using Python and win32, but whenever Exchange address is found, I am unable to get the SMTP address. I tried everything available on the net, but it is returning an empty string. Here is the code snippet I'm using:
if msg.Class == 43:
if msg.SenderEmailType == 'EX':
print(msg.Sender.GetExchangeUser().PrimarySmtpAddress)
else:
print(msg.SenderEmailAddress)
You can try to read PidTagSenderSmtpAddress
MAPI property (DASL name http://schemas.microsoft.com/mapi/proptag/0x5D01001F
) using MailItem.PropertyAccessor.GetProperty
- take a look at the message with OutlookSpy (click IMessage button) or MFCMAPI.
Using OutlookSpy I have been able to find the property I need, which is
PidTagSenderSmtpAddress_W
(yeah, very close to what you mentioned). However the object and the property are incompatible. Hence I need to add something so that the property is compatible. Please help me with the last step.GetProperty() returns generic
object
', Cast it tostring
.I suppose instead of
MailItem
I should specify the item itself, i.e.,msg
in my case?Yes. MailItem is the COM object class name - docs.microsoft.com/en-us/office/vba/api/outlook.mailitem
Presently I am doing
msg.PropertyAccessor.GetProperty.PidTagSenderSmtpAddress_W
, and the error shown isAttributeError: 'function' object has no attribute 'PidTagSenderSmtpAddress_W'
. And looking at the error, I am not very confident that casting theobject
tostring
will be helpful.