I'm using c# and Microsoft Outlook interop outlook dll to read and retrieve mails from Outlook (Exchange).
I'm facing issues in parsing the sender email address as the code returns the Exchange address rather than the plain email id. What i want to retrieve is like username@abc.com
but the output of my code is the Exchange address of the email:
(/O=EXCHANGELABS/OU=EXCHANGE ADMINISTRATIVE GROUP (xxxxxxxxxxx)/CN=RECIPIENTS/CN=XXXXXXX32AD740E69184DC03B2A406F4-XXX XXX)
Code I have tried:
emailItem.Sender
and
emailItem.SenderEmailAddress
How do I convert the Exchange address to an smtp address or how do I retrieve just the smtp address?
This code should do it:
string senderEmailAddress;
if (mi.SenderEmailType == "EX")
{
senderEmailAddress = emailItem.Sender.GetExchangeUser().PrimarySmtpAddress;
}
else
{
senderEmailAddress = emailItem.SenderEmailAddress;
}
Where:
emailItem
is a MailItemClass
emailItem.SenderEmailType
is a string representation of the type of the e-mail address of the sender of the Outlook item
the Sender
property returns an AddressEntry
GetExchangeUser
returns an ExchangeUser
object that represents the AddressEntry
if the address corresponds to an Exchange user
And from there we can retrieve the PrimarySmtpAddress
( string
).
This should work with Outlook 2010 and newer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.