Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to: Get the SMTP Address of the Sender of a Mail Item

This example gets the sender’s Simple Mail Transfer Protocol (SMTP) address for a mail item.

To determine the SMTP address for a received mail item, use the SenderEmailAddress property of the MailItem object. However, if the sender is internal to your organization, SenderEmailAddress does not return an SMTP address, and you must use the PropertyAccessor object to return the sender’s SMTP address.

In the following code example, GetSenderSMTPAddress uses the PropertyAccessor object to obtain values that are not exposed directly in the Outlook object model. GetSenderSMTPAddress takes in a MailItem. If the value of the SenderEmailType property of the received MailItem is "EX", the sender of the message resides on an Exchange server in your organization. GetSenderSMTPAddress uses the Sender property of the MailItem object to get the sender, represented by the AddressEntry object. If the AddressEntry object represents an Exchange user, the example calls the GetExchangeUser() method to return the ExchangeUser object of the AddressEntry object. GetSenderSMTPAddress then uses the PrimarySmtpAddress property of the ExchangeUser object to return the SMTP address of the sender. If the AddressEntry object for the sender does not represent an ExchangeUser object, the GetProperty(String) method of the PropertyAccessor object is used, with PR_SMTP_ADDRESS (PidTagSmtpAddress) as the argument, to return the sender’s SMTP address.

If you use Microsoft Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 14.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private string GetSenderSMTPAddress(Outlook.MailItem mail)
{
    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;
    }
}

Other Resources

Show:
© 2014 Microsoft