We are developing an Outlook 365 plugin that retrieves various parts of the email to create a file. When the mail in the inbox is sent with exchange we are not able to retrieve the sender. We copied a method in the microsoft manual page but it always returns null.
This is the method:
private string GetSenderSMTPAddress(Microsoft.Office.Interop.Outlook.MailItem mail)
{
string PR_SMTP_ADDRESS =
@"http://schemas.microsoft.com/mapi/proptag/0x39FE001E";
if (mail == null)
{
throw new ArgumentNullException();
}
if (mail.SenderEmailType == "EX")
{
Microsoft.Office.Interop.Outlook.AddressEntry sender =
mail.Sender;
if (sender != null)
{
//Now we have an AddressEntry representing the Sender
if (sender.AddressEntryUserType ==
Microsoft.Office.Interop.Outlook.OlAddressEntryUserType.
olExchangeUserAddressEntry
|| sender.AddressEntryUserType ==
Microsoft.Office.Interop.Outlook.OlAddressEntryUserType.
olExchangeRemoteUserAddressEntry)
{
//Use the ExchangeUser object PrimarySMTPAddress
Microsoft.Office.Interop.Outlook.ExchangeUser exchUser =
sender.GetExchangeUser();
if (exchUser != null)
{
return exchUser.PrimarySmtpAddress;
}
else
{
return null;
}
}
else
{
return sender.PropertyAccessor.GetProperty(
PR_SMTP_ADDRESS) as string;
}
}
else
{
return null;
}
}
else
{
return mail.SenderEmailAddress;
}
}
What can we do to retrieve the sender ? It seems that that manual page is outdated
In any case the step that returns null is this:
if (exchUser != null)
{
return exchUser.PrimarySmtpAddress;
}
The sender-related properties are set only after the email has been sent. Make sure that you deal with sent or received items in Outlook. For that you can check the
MessageFlagsbitmask for theMSGFLAG_UNSENTflag.