问题
I am using get_SenderEmailAddress() of Outlook::_MailItem object to get sender's email address. But if user is an active directory user, then recipientitem.address looks like this: /o=organizationg/ou=exchange administrative group /cn=recipients/cn=xxxxxxxxxx.
Is there any other way to get sender's email address?
回答1:
I am using this to get the Sender Mail Address.
private string GetSenderSMTPAddress(Outlook.MailItem mail)
{
try
{
string PR_SMTP_ADDRESS =
@"http://schemas.microsoft.com/mapi/proptag/0x39FE001E"; if (mail == null)
{
throw new ArgumentNullException();
}
if (mail.SenderEmailType == "EX")
{
Outlook.AddressEntry sender =
mail.Sender;
if (sender != null)
{
//Now we have an AddressEntry representing the Sender
if (sender.AddressEntryUserType ==
Outlook.OlAddressEntryUserType.
olExchangeUserAddressEntry
|| sender.AddressEntryUserType ==
Outlook.OlAddressEntryUserType.
olExchangeRemoteUserAddressEntry)
{
//Use the ExchangeUser object PrimarySMTPAddress
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;
}
}
catch (Exception ex)
{
return null;
}
}
回答2:
That looks like a perfectly valid email address of type "EX"
(as opposed to "SMTP").
If you want the SMTP address, use MailItem.Sender.GetExchangeUser().PrimarySmtpAddress
. Be prepared to handle nulls and exceptions.
But first check the MailItems.SenderEmailType
property - if it is "SMTP", you can still use SenderEmailAddress
.
来源:https://stackoverflow.com/questions/44178822/how-to-get-senders-email-address-if-user-is-an-active-directory-user