How to get Sender Email Address from MailItem in Exchange Accounts?

For getting Sender Email of a MailItem in POP3/SMTP accounts if pretty easy. We can get that by olMailItem.SenderEmailAddress. But, for Exchange Accounts, its  a bit different. We need to get the AddressEntry first and then get the SMTP Address from that object. Here is a sample code that I framed for simpler understanding.

Dim objSender As Outlook.Recipient = Nothing
Dim objPropXR As Outlook.PropertyAccessor = Nothing
Dim strSMTPAddress As String = “”
Dim strSenderEntryID As String = “”
Dim objNS As Outlook.NameSpace = Nothing
Dim objAE As Outlook.AddressEntry = Nothing
Dim objEU As Outlook.ExchangeUser = Nothing

objNS = Globals.ThisAddIn.Application.GetNamespace(“MAPI”)
objPropXR = objMail.PropertyAccessor
strSenderEntryID = objPropXR.BinaryToString(objPropXR.GetProperty(“http://schemas.microsoft.com/mapi/proptag/0x0C190102”))
objSender = objNS.GetRecipientFromID(strSenderEntryID)
objAE = objSender.AddressEntry
If objAE.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry Then
objEU = objAE.GetExchangeUser
strSMTPAddress = objEU.PrimarySmtpAddress
Else
strSMTPAddress = objAE.Address
End If
MessageBox.Show (“Sender Email Address: ” & strSMTPAddress)

objMailItem is an object of Outlook.MailItem

Hope this helps!

Advertisements
This entry was posted in Office12 and tagged , , , by Pranav. Bookmark the permalink.

About Pranav

A Software Engineer by profession and a Geek at heart. I love exploring new technologies and working on algorithmic problems. As of now, I write code for bread, butter and satisfaction. I like to develop applications, and generally that involves much more than writing code. It’s not just about hurling code, but producing something meaningful that has a measurable and positive impact, helps others and solves interesting problems.

4 thoughts on “How to get Sender Email Address from MailItem in Exchange Accounts?

  1. Hi Pranev

    many thanks once again for your help. Please tell me, because you have a web link in this code will it only work if the user is connected to the internet?

    thanks

    Steve

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s