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.
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.
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);
}
}
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.
}
}
}
}
}
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.
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.
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.