I recently came across an issue where I needed to get the details about Exchange User from within an Outlook Add-in. Below is a screen shot of the details I am referring to. In Outlook 2007 its very simple, just use the ExchangeUser Object and you will get the details. More details can be found in the article below:
ExchangeUser Object Members http://msdn.microsoft.com/en-us/library/bb176658.aspx
Below is sample C# code that shows how to use the ExchangeUser Object:
using System; using System.Collections.Generic; using System.Text; using Outlook = Microsoft.Office.Interop.Outlook; namespace GetUserDetails { class Program { static void Main(string[] args) { Outlook.Application oApp; Outlook.NameSpace oNameSpace; Outlook.SelectNamesDialog oSNDialog; Outlook.AddressEntry oAEntry; Outlook.ExchangeUser oEUser; oApp = new Outlook.Application(); oNameSpace = oApp.GetNamespace("MAPI"); oNameSpace.Logon(null, null, true, false); oSNDialog = oNameSpace.GetSelectNamesDialog(); oSNDialog.NumberOfRecipientSelectors = Outlook.OlRecipientSelectors.olShowTo; oSNDialog.Caption = "Select User"; oSNDialog.ToLabel = "User Name"; oSNDialog.ShowOnlyInitialAddressList = true; oSNDialog.AllowMultipleSelection = false; oSNDialog.Display(); foreach(Outlook.Recipient oRecipient in oSNDialog.Recipients) { oAEntry = oRecipient.AddressEntry; if(oAEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry || oAEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry) { oEUser = oAEntry.GetExchangeUser(); Console.WriteLine(oEUser.Name); Console.WriteLine(oEUser.BusinessTelephoneNumber); Console.WriteLine(oEUser.MobileTelephoneNumber); Console.WriteLine(oEUser.Department); oEUser = null; } oAEntry = null; } oNameSpace.Logoff(); oNameSpace = null; } } }
However in Outlook 2003 the ExchangeUser Object is not there. The only way to do it is query the AD based on the AddressEntry.Address(LegacyexchangeDN). Below is sample C# code that uses the DirectoryServices namespace to query the AD based on LegacyexchangeDN.
using System; using System.Collections.Generic; using System.Text; using System.DirectoryServices; namespace GetUserDetailsBasedOnLegacyDN { class Program { static void Main(string[] args) { //This is the legacy exchange DN string LEDN = "/O=ms/OU=east/cn=Recipients/cn=whatever"; DirectoryEntry objRootDSE = new DirectoryEntry("GC://RootDSE"); String strRoot = objRootDSE.Properties["rootDomainNamingContext"].Value.ToString(); DirectoryEntry objRoot = new DirectoryEntry("GC://" + strRoot); objRoot.RefreshCache(); DirectorySearcher objSrch = new DirectorySearcher(objRoot); objSrch.Filter = "(&(objectClass=user)(objectCategory=person)(legacyExchangeDN= " + LEDN + "))"; objSrch.SearchScope = SearchScope.Subtree; DirectoryEntry objUser = new DirectoryEntry(); objUser = objSrch.FindOne().GetDirectoryEntry(); Console.WriteLine("Mobile:" + objUser.Properties["mobile"].Value); Console.WriteLine("Number:" + objUser.Properties["telephoneNumber"].Value); Console.WriteLine("Department:" + objUser.Properties["department"].Value); objUser = null; objSrch = null; objRoot = null; objRootDSE = null; } } }