HOWTO: EWS : Get SMTP address from X500 address

Getting SMTP address from X500 address was a tricky task in exchange 2003 days.

But with the release of exchange 2007 not only you can get SMTP address but other details as well from Active Directory using EWS and that's too in one single call.

Today I will introduce you with a magic function of Exchange 2007 Web Services i.e. ResolveNames

MSDN: https://msdn2.microsoft.com/en-us/library/aa563518.aspx

"This operation can be used to verify aliases and resolve display names to the appropriate mailbox user. If ambiguous names exist, the ResolveNames response provides information about each mailbox user so that the client application can resolve the names.

The ResolveNames response returns a maximum of 100 candidates. The 100 candidates that are returned are the first 100 that are encountered in the lookup operation.

Only one ambiguous name can be specified in a single request. Active Directory is searched first, and then the user's contact folder is searched. Resolved entries from a user's contact folder have a non-null ItemId property, which can be used then in a GetItem request. If it is the ID of a private distribution list, then it can be used in an ExpandDL Operation. If the ReturnFullContactData attribute is set to true, then Active Directory entries found with the ResolveNames Operation will return additional properties describing a Contact. The ReturnFullContactData attribute does not affect the data returned for contacts and private distribution lists from the user's contact folder."

Sample Code in C#

// Assumption: You are running this code from VS2005

// and already added the Web Reference to the EWS into your project,

// also added the necessary EWS namespaces to the class

            ExchangeServiceBinding esb = new ExchangeServiceBinding();

// Provide the credential to connect to Exchange Server

esb.Credentials = new System.Net.NetworkCredential("username", "password", "domain");

            esb.AllowAutoRedirect = true;

            esb.Url = "https://Exchange2007_CAS_Server/EWS/Exchange.asmx";

            // Create the ResolveNamesType and set the unresolved entry.

            ResolveNamesType rnRequest = new ResolveNamesType();

            rnRequest.ReturnFullContactData = true;

            rnRequest.UnresolvedEntry = "/o=My First Organization/ou=Test Organizational Unit/cn=Recipients/cn=TestEwsUser";

            // Send the request and get the response.

            ResolveNamesResponseType rnrt = esb.ResolveNames(rnRequest);

            ResolveNamesResponseMessageType rnrmt = ((ResolveNamesResponseMessageType)rnrt.ResponseMessages.Items[0]);

// Assuming that only one result is returned, you can use for loop to loop through all the results

            ResolutionType rt = rnrmt.ResolutionSet.Resolution[0];

// Send the request and get the response.

// You can check if rt.Mailbox.RoutingType == "SMTP" to make sure you are reading the SMTP address, in normal scenarios it will always be SMTP

            Console.WriteLine(rt.Mailbox.EmailAddress);

 

Keywords: ResolveNames, Exchange Web Services, Exchange 2007, X500 to SMTP