I'm trying to write a program that finds my e-mails. I'm having trouble writing a function that would return the sender's email address. Whatever type of message it is. I find function:
def return_sender(msg):
if msg.Class == 43
if msg.SenderEmailType == "EX":
if msg.Sender.GetExchangeUser() != None:
return msg.Sender.GetExchangeUser().PrimarySmtpAddress
else:
return msg.Sender.GetExchangeDistributionList().PrimarySmtpAddress
else:
return msg.SenderEmailAddress
Class 43 is MailItem, but for example how to get the sender's address from class 53 = MeetingItem?
msg.Sender
It returns AttributeError.
msg.SenderEmailAddress
It returns:
/O=EXCHANGELABS/OU=EXCHANGEADMINISTRATIVEGROUP(FYDIBOHF23SPDLT)/CN=RECIPIENTS/CN=6F467C825619482293F429C0BDE6F1DB-
Is there any way to get the sender from a message type other than MailItem? And how you can check outlook what type of message is Thank you
CodePudding user response:
You need to convert an Exchange formatted X509 email address into SMTP one. Read more about that in the HowTo: Convert Exchange-based email address into SMTP email address article.
Here is one of the possible ways:
/// <summary>
/// Finds the SMTP addres based on an exchange string inside an outlook contact
/// </summary>
/// <param name="">Has to be an exchange address string</param>
/// <returns></returns>
private string GetSMTPAddressViaOutlookObjectModel(string exchangeAddress)
{
// Start with an empty string
var address = string.Empty;
// Create a recipients with the exchange address
// E.g. .....
Outlook.Recipient recipient = Module.OutlookApp.Session.CreateRecipient(exchangeAddress);
// Resolve the recipient
recipient.Resolve();
// When the recipient is resolved
if (recipient.Resolved)
{
// Get the user behind the resolved address
Outlook.ExchangeUser user = recipient.AddressEntry.GetExchangeUser();
// If the user is found
if (user != null)
{
// set the address to the primary SMTP address of the exchange user
address = user.PrimarySmtpAddress;
// Release the user object
Marshal.ReleaseComObject(user);
}
// Release the recipient object
Marshal.ReleaseComObject(recipient);
}
// return the address
return address;
}
CodePudding user response:
You can retrieve any MAPI property using ReportItem/MeetingItem.PropetyAccessor.GetProperty
, you just need to know the property's DASL name. E.g. PR_SENT_REPRESENTING_SMTP_ADDRESS
(may or may not be available on a particular item) is "http://schemas.microsoft.com/mapi/proptag/0x5D02001F"
. Take a look at the report/meeting item with OutlookSpy (I am its author) - select the item in question, click IMessage button on the OutlookSpy ribbon, select the property you need, look at the DASL edit box.