none
[E2010][EWS Managed API][C#]how can i retrieve all user emails(mailboxes) from exchange server RRS feed

  • Question

  • hey,

    i currently making a program for an admin in which he wants to be able to retrieve all the user emails(mailboxes) so he can send emails to the mailboxes he wishes

    i'm using ews managed api

    Tuesday, January 18, 2011 1:58 PM

Answers

  • I don't think you can do this sort of thing in EWS, which is generally used to access mailbox data, not user details.  You'd have to get this information by querying Active Directory.
    Outlook Web Access For PDA , OWA For WAP
    www.owa-pda.com
    email a@t leederbyshire d.0.t c.0.m
    • Proposed as answer by Jirka_ Tuesday, January 18, 2011 3:29 PM
    • Marked as answer by SamuraiJack Sunday, January 23, 2011 11:41 AM
    Tuesday, January 18, 2011 3:13 PM
  • Lee's response is correct - you can't do what you want with EWS.  This is a task for Active Directory.  I'm going to post code below that may or may not help you.  In order to understand what's going on, I would STRONGLY suggest you get a copy of:  The .Net Developer's Guide to Directory Services Programming, by Joe Kaplan and Ryan Dunn.  This book will teach you everything you need to know to program against AD using C#.

     

    And now, for my code:

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #008010} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #808080} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; min-height: 14.0px} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #00f628} p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #008080} p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #c0c0c0} p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #fff82d} span.s1 {color: #808080} span.s2 {color: #008010} span.s3 {color: #fff82d} span.s4 {color: #008080} span.s5 {color: #c0c0c0} span.s6 {color: #000000} span.Apple-tab-span {white-space:pre}

    /// <summary>

    /// Using Active Directory, returns a list of Exchange Mailboxes.  Note that since AD is being used and not Powershell, some 

    /// mailbox properties may not get populated.

    /// </summary>

    /// <param name="mailboxDatabaseDN">Distinguished Name of mailbox database.  If not null or empty, the returned list

    /// will only include mailboxes found on the named database.</param>

    /// <param name="iMaxReturnedCount">Can be used to restrict the number of mailboxes returned.  If less than or equal to zero,

    /// we return all mailboxes.</param>

    /// <param name="bADUACEnabled">If set to true, we use the values specified in 'IsAccountValid()' to determine if a mailbox

    /// should be included in the returned list.</param>

    /// <param name="strErrorMessage"></param>

    /// <returns></returns>

    static public List<Mailbox> GetMailboxesFromActiveDirectory(string mailboxDatabaseDN, int iMaxReturnedCount, bool bADUACEnabled, out string strErrorMessage)

    {

    strErrorMessage = "";

    List<Mailbox> mailboxList = new List<Mailbox>();

     

     

     

    // First, we're going to need an indexed list of the Exchange Servers (to be used when populating each Mailbox)

    List<ExchangeServer> exchangeServers = GetExchangeServersFromActiveDirectory(nullout strErrorMessage);

    Dictionary<stringExchangeServer> serversMap = new Dictionary<stringExchangeServer>(StringComparer.CurrentCultureIgnoreCase);

    foreach (ExchangeServer myServer in exchangeServers)

    {

    serversMap.Add(myServer.ExchangeLegacyDN, myServer);

    }

     

     

     

    // Where to start our search...

    Domain myDomain = Domain.GetCurrentDomain();

    using (myDomain)

    {

    using (DirectoryEntry searchRoot = myDomain.GetDirectoryEntry())

    {

    using (DirectorySearcher dirSearcher = new DirectorySearcher(searchRoot))

    {

    if (String.IsNullOrEmpty(mailboxDatabaseDN))

    {

    dirSearcher.Filter = "(&(objectClass=user)(objectCategory=person))";

    }

    else

    {

    dirSearcher.Filter = String.Format("(&(objectClass=user)(objectCategory=person)(homeMDB={0}))", mailboxDatabaseDN);

    }

     

    // Properties of interest...

    dirSearcher.PropertiesToLoad.Add("sAMAccountName");

    dirSearcher.PropertiesToLoad.Add("displayName");

    dirSearcher.PropertiesToLoad.Add("adsPath");

    dirSearcher.PropertiesToLoad.Add("homeMDB");

    dirSearcher.PropertiesToLoad.Add("mailNickname");

    dirSearcher.PropertiesToLoad.Add("msExchHomeServername");

    dirSearcher.PropertiesToLoad.Add("mail");

    dirSearcher.PropertiesToLoad.Add("ProxyAddresses");

    dirSearcher.PropertiesToLoad.Add("distinguishedName");

    dirSearcher.PropertiesToLoad.Add("userPrincipalName");

    dirSearcher.PropertiesToLoad.Add("givenName");

    dirSearcher.PropertiesToLoad.Add("name");

    dirSearcher.PropertiesToLoad.Add("userAccountControl");

     

     

    // Search!

    SearchResult searchResult;

    SearchResultCollection resultCol = dirSearcher.FindAll();

    if (resultCol == null)

    return mailboxList;

     

    for (int iLoop = 0; iLoop < (iMaxReturnedCount <= 0 ? resultCol.Count : iMaxReturnedCount); iLoop++)

    {

    searchResult = resultCol[iLoop];

     

    string mailNickname = ExtractStringFromSearchResult("mailNickname", searchResult.Properties);

    string homeMDB = ExtractStringFromSearchResult("homeMDB", searchResult.Properties);

    string displayName = ExtractStringFromSearchResult("displayName", searchResult.Properties);

    string name = ExtractStringFromSearchResult("name", searchResult.Properties);

    string mail = ExtractStringFromSearchResult("mail", searchResult.Properties);

    string sAMAccountName = ExtractStringFromSearchResult("sAMAccountName", searchResult.Properties);

    string userAccountControl = ExtractStringFromSearchResult("userAccountControl", searchResult.Properties);

    bool bOk = ((bADUACEnabled) ? IsAccountValid(userAccountControl) : true);

     

     

    if (String.IsNullOrEmpty(homeMDB) || !bOk)

    {

    continue;

    }

     

    Mailbox theMailbox = new Mailbox

    (

    mailNickname,

    homeMDB,

    displayName,

    name,

    mail,

    sAMAccountName

    );

     

    // The mailbox's home server...

    ExchangeServer mailboxServer;

    if (serversMap.TryGetValue(ExtractStringFromSearchResult("msExchHomeServername", searchResult.Properties), out mailboxServer))

    {

    theMailbox.ServerName = mailboxServer.Name;

    }

     

    // Add to the returned list...

    mailboxList.Add(theMailbox);

    }

    }

    }

    }

    return mailboxList;

    }

    • Marked as answer by SamuraiJack Sunday, January 23, 2011 11:41 AM
    Thursday, January 20, 2011 7:11 PM

All replies

  • I don't think you can do this sort of thing in EWS, which is generally used to access mailbox data, not user details.  You'd have to get this information by querying Active Directory.
    Outlook Web Access For PDA , OWA For WAP
    www.owa-pda.com
    email a@t leederbyshire d.0.t c.0.m
    • Proposed as answer by Jirka_ Tuesday, January 18, 2011 3:29 PM
    • Marked as answer by SamuraiJack Sunday, January 23, 2011 11:41 AM
    Tuesday, January 18, 2011 3:13 PM
  • for querying Active Directory would i need anther library ?

    i've been reading the msdn documentation i think it could be done using the management sdk

    but i can't use any other sdk except the managed one

    i downloaded them then setup them and then added a reference to the Services.wsdl in VS2008

    but i still can't use them 

    Wednesday, January 19, 2011 1:05 PM
  • I've only ever done this in VBScript in Notepad, so I don't get to use SDKs and libraries.  A search for 'list active directory users in c#' returns things like this:

    http://www.netomatix.com/EnumADUsers.aspx


    Outlook Web Access For PDA , OWA For WAP
    www.owa-pda.com
    email a@t leederbyshire d.0.t c.0.m
    Wednesday, January 19, 2011 3:03 PM
  • Lee's response is correct - you can't do what you want with EWS.  This is a task for Active Directory.  I'm going to post code below that may or may not help you.  In order to understand what's going on, I would STRONGLY suggest you get a copy of:  The .Net Developer's Guide to Directory Services Programming, by Joe Kaplan and Ryan Dunn.  This book will teach you everything you need to know to program against AD using C#.

     

    And now, for my code:

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #008010} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #808080} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas} p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; min-height: 14.0px} p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #00f628} p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #008080} p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #c0c0c0} p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Consolas; color: #fff82d} span.s1 {color: #808080} span.s2 {color: #008010} span.s3 {color: #fff82d} span.s4 {color: #008080} span.s5 {color: #c0c0c0} span.s6 {color: #000000} span.Apple-tab-span {white-space:pre}

    /// <summary>

    /// Using Active Directory, returns a list of Exchange Mailboxes.  Note that since AD is being used and not Powershell, some 

    /// mailbox properties may not get populated.

    /// </summary>

    /// <param name="mailboxDatabaseDN">Distinguished Name of mailbox database.  If not null or empty, the returned list

    /// will only include mailboxes found on the named database.</param>

    /// <param name="iMaxReturnedCount">Can be used to restrict the number of mailboxes returned.  If less than or equal to zero,

    /// we return all mailboxes.</param>

    /// <param name="bADUACEnabled">If set to true, we use the values specified in 'IsAccountValid()' to determine if a mailbox

    /// should be included in the returned list.</param>

    /// <param name="strErrorMessage"></param>

    /// <returns></returns>

    static public List<Mailbox> GetMailboxesFromActiveDirectory(string mailboxDatabaseDN, int iMaxReturnedCount, bool bADUACEnabled, out string strErrorMessage)

    {

    strErrorMessage = "";

    List<Mailbox> mailboxList = new List<Mailbox>();

     

     

     

    // First, we're going to need an indexed list of the Exchange Servers (to be used when populating each Mailbox)

    List<ExchangeServer> exchangeServers = GetExchangeServersFromActiveDirectory(nullout strErrorMessage);

    Dictionary<stringExchangeServer> serversMap = new Dictionary<stringExchangeServer>(StringComparer.CurrentCultureIgnoreCase);

    foreach (ExchangeServer myServer in exchangeServers)

    {

    serversMap.Add(myServer.ExchangeLegacyDN, myServer);

    }

     

     

     

    // Where to start our search...

    Domain myDomain = Domain.GetCurrentDomain();

    using (myDomain)

    {

    using (DirectoryEntry searchRoot = myDomain.GetDirectoryEntry())

    {

    using (DirectorySearcher dirSearcher = new DirectorySearcher(searchRoot))

    {

    if (String.IsNullOrEmpty(mailboxDatabaseDN))

    {

    dirSearcher.Filter = "(&(objectClass=user)(objectCategory=person))";

    }

    else

    {

    dirSearcher.Filter = String.Format("(&(objectClass=user)(objectCategory=person)(homeMDB={0}))", mailboxDatabaseDN);

    }

     

    // Properties of interest...

    dirSearcher.PropertiesToLoad.Add("sAMAccountName");

    dirSearcher.PropertiesToLoad.Add("displayName");

    dirSearcher.PropertiesToLoad.Add("adsPath");

    dirSearcher.PropertiesToLoad.Add("homeMDB");

    dirSearcher.PropertiesToLoad.Add("mailNickname");

    dirSearcher.PropertiesToLoad.Add("msExchHomeServername");

    dirSearcher.PropertiesToLoad.Add("mail");

    dirSearcher.PropertiesToLoad.Add("ProxyAddresses");

    dirSearcher.PropertiesToLoad.Add("distinguishedName");

    dirSearcher.PropertiesToLoad.Add("userPrincipalName");

    dirSearcher.PropertiesToLoad.Add("givenName");

    dirSearcher.PropertiesToLoad.Add("name");

    dirSearcher.PropertiesToLoad.Add("userAccountControl");

     

     

    // Search!

    SearchResult searchResult;

    SearchResultCollection resultCol = dirSearcher.FindAll();

    if (resultCol == null)

    return mailboxList;

     

    for (int iLoop = 0; iLoop < (iMaxReturnedCount <= 0 ? resultCol.Count : iMaxReturnedCount); iLoop++)

    {

    searchResult = resultCol[iLoop];

     

    string mailNickname = ExtractStringFromSearchResult("mailNickname", searchResult.Properties);

    string homeMDB = ExtractStringFromSearchResult("homeMDB", searchResult.Properties);

    string displayName = ExtractStringFromSearchResult("displayName", searchResult.Properties);

    string name = ExtractStringFromSearchResult("name", searchResult.Properties);

    string mail = ExtractStringFromSearchResult("mail", searchResult.Properties);

    string sAMAccountName = ExtractStringFromSearchResult("sAMAccountName", searchResult.Properties);

    string userAccountControl = ExtractStringFromSearchResult("userAccountControl", searchResult.Properties);

    bool bOk = ((bADUACEnabled) ? IsAccountValid(userAccountControl) : true);

     

     

    if (String.IsNullOrEmpty(homeMDB) || !bOk)

    {

    continue;

    }

     

    Mailbox theMailbox = new Mailbox

    (

    mailNickname,

    homeMDB,

    displayName,

    name,

    mail,

    sAMAccountName

    );

     

    // The mailbox's home server...

    ExchangeServer mailboxServer;

    if (serversMap.TryGetValue(ExtractStringFromSearchResult("msExchHomeServername", searchResult.Properties), out mailboxServer))

    {

    theMailbox.ServerName = mailboxServer.Name;

    }

     

    // Add to the returned list...

    mailboxList.Add(theMailbox);

    }

    }

    }

    }

    return mailboxList;

    }

    • Marked as answer by SamuraiJack Sunday, January 23, 2011 11:41 AM
    Thursday, January 20, 2011 7:11 PM
  • yup that did it 

    thanks very much Lee Derbyshire and Rob Jones

    and i'll definitely read that book 

    there is anther problem 

    the program connects logs the user in using Autodiscverurl

    then when i try to send a message it prompts an error "Unable to access an account or mailbox" at the send command

    Sunday, January 23, 2011 11:41 AM