Question

I am making a web program designed in Visual Studio 2013, ASP, C#.

I am looking for a way to be able to search the Active Directory to obtain email addresses to send automated emails when a report is generated. My requirement is to obtain the email address to individuals based on their job title and their location (i.e. New York, Dallas, etc.).

I was thinking along the lines of creating two lists (one for the TO: addresses and the other for the CC: addresses). I was going to use lists because the amount of people that will be getting the emails vary based on the site location and type of report.

My problem is I do not know how to set up the search parameters in order to populate these lists. Research I have done indicates that I will need to use System.DirectoryServices to create a DirectorySearcher.

All this is going to be done under the "Submit Report" button click event.

Was it helpful?

Solution

Create a filter for the properties you need:

     string filter = @"(&(objectCategory=Person)(physicalDeliveryOfficeName=Dallas))";

Then search for the mail addresses:

     List<string> list = GetMail(filter);

.

  List<string> GetMail(string SearchFilter)
  {
     List<string> MailAddresses = new List<string>();
      using (DirectorySearcher directorySearcher = new DirectorySearcher())
      {
      directorySearcher.Filter = SearchFilter;
      SearchResultCollection resultCollection = directorySearcher.FindAll();

     foreach (SearchResult searchResult in resultCollection)
      {
         try
         {
            MailAddresses.Add(searchResult.Properties["mail"][0].ToString());
         }
         catch {
                //Maybe fill a list of errors here.
               }
      } 
  }
     return MailAddresses;
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top