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: http://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

Comments (6)

  1. Peter Olsson says:

    Another great example 🙂

    One thing makes me wonder though – you say that it will return at most 100 objects. What would be the best approach to get info about all mailboxes, on a specific Exchange server, even if it’s more than 100?

    I’m still coding on my push-subscription handler. And when I start this application I want to get all mailboxes that are available on the specific Exchange Server.

    Regards,

    Peter Olsson

  2. Vikas - MSFT says:

    Hi Peter,

    Unfortunately there is no possible way to list down all the mailboxes using EWS. Although I have already suggested this feature to the product group and they may include this in future SP releases or next version of exchange, which I cannot comment on.

    Regarding the best approach to list down all the Mailboxes programmatically, you can use Powershell from .net to execute “Get-MailBox” cmdlet on exchange server and get the response. Since AFAIK Powershell cmdlets cannot be executed remotely, you need to create a web service on exchange server which will execute this Powelshell code for you.

    See following blog for some great links to programming Powershell from .net

    http://blogs.msdn.com/vikas/archive/2007/06/19/powershell-net.aspx

    Regards,

    Vikas Verma

  3. Peter Olsson says:

    Thanks!

    I’ll give it a try 🙂

    Regards,

    Peter Olsson

  4. Getting primary address from X500 address was always a pain when using OOM. Thank to these guys who realized

  5. Vikas Verma says:

    HOWTO: OOM: Getting Primary SMTP Address from X500

  6. I’ve put together a list of articles which cover common questions on Exchange Web Services (EWS). These

Skip to main content