views:

807

answers:

2

How do i get a list of all e-mail address for exchange public folders?

Will reply on my own, will accept the best reply offered.

A: 

The following code will get a list of all email address of public folders in exchange.

public static void GetPublicFolderList()
{
 DirectoryEntry entry = new DirectoryEntry("LDAP://FakeDomain.com");
 DirectorySearcher mySearcher = new DirectorySearcher(entry);
 mySearcher.Filter = "(&(objectClass=publicfolder))";
 mySearcher.SizeLimit = int.MaxValue;
 mySearcher.PageSize = int.MaxValue;            

 foreach (SearchResult resEnt in mySearcher.FindAll())
 {
  if (resEnt.Properties.Count == 1)
   continue;

  object OO = resEnt.Properties["mail"][0];
 }
}

If you want All email addresses of the public folder,

remove:

object OO = resEnt.Properties["mail"][0];

Add: for (int counter = 0; counter < resEnt.Properties["proxyAddresses"].Count; counter++)

{
 string Email = (string)resEnt.Properties["proxyAddresses"][counter];
 if (Email.ToUpper().StartsWith("SMTP:"))
 {
  Email = Email.Remove(0, "SMTP:".Length);
 }
}
EKS
you answered your own question in less than 1 min..
jinsungy
Correct, i knew the reply before i posted it. It was not about learning how to do it, it was getting a proper reply so the next time someone has to search for it they find a reply faster then what i did
EKS
You're still not accounting for disposing of the DirectorySearcher and if the entry doesn't contain a "proxyAddresses" attribute it will throw a NullReferenceException.
Joshua
resEnt.Properties.Count == 1 <- Does that. This code does work, but your reply is has more description and will therefor be accepted.
EKS
+2  A: 

While what you posted as your own answer would work, it helps to read the documentation for the methods and objects you are using to understand their limitations. If you had called this code multiple times you would eventually had a memory leak on your hands. The foreach statement doesn't call Dispose() on the object used, only the enumerator it creates. Below is a somewhat better method of searching the directory (though very little error checking and no exception handling).

public static void GetPublicFolderList()
{
    DirectoryEntry entry = new DirectoryEntry("LDAP://sorcogruppen.no");
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = "(&(objectClass=publicfolder))";
    // Request the mail attribute only to reduce the ammount of traffic
    // between a DC and the application.
    mySearcher.PropertiesToLoad.Add("mail");

    // See Note 1
    //mySearcher.SizeLimit = int.MaxValue;

    // No point in requesting all of them at once, it'll page through
    // all of them for you.
    mySearcher.PageSize = 100;

    // Wrap in a using so the object gets disposed properly.
    // (See Note 2)
    using (SearchResultCollection searchResults = mySearcher.FindAll())
    {
        foreach (SearchResult resEnt in searchResults)
        {
            // Make sure the mail attribute is provided and that there
            // is actually data provided.
            if (resEnt.Properties["mail"] != null
                 && resEnt.Properties["mail"].Count > 0)
      {
                string email = resEnt.Properties["mail"][0] as string;
                if (!String.IsNullOrEmpty(email))
                {
                    // Do something with the email address
                    // for the public folder.
                }
            }
        }
    }
}

Note 1

The remarks for DirectorySearcher.SizeLimit indicate that the size limit is ignored if it is higher than the server-determined default (1000 entries). Paging allows you to get all of the entries you need as you need them.

Note 2

The remarks for DirectorySearcher.FindAll() mention that the SearchResultCollection needs to be disposed to release resources. Wrapping it in a using statement clearly identifies your intent as a programmer.

Extra

If you're using Exchange 2007 or 2010 you could also install the Exchange Management Tools and use the powershell cmdlets to query your public folders. You can pragmatically create a powershell runspace and call the Exchange cmdlets directly without actually needing a console for the user to interact with.

Joshua