Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How do I get the emails of all the members of a given group in ActiveDirectory - Need help

How do I get the emails of all the members of a given group in ActiveDirectory - Need help

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Slow Learner
    wrote on last edited by
    #1

    Hi, I have a need to send out emails to all the people who are in a given group say "HR Department". I need to be able to get all the emails through a for loop and then as it retrieves I should send emails to them. Can some one please help me with the code how to get the emails out of AD. this is what I came up with but not sure how to go about writing a for loop to get all the emails in the given group. I am having difficulty to send both the loginName and the group that I am interested in to the AD in the search.Filter. public static string SendEmails(string loginName) { string Department = "HR Department" string userName = ExtractUserName(loginName); DirectorySearcher search = new DirectorySearcher(); search.Filter = String.Format("(SAMAccountName={0})", userName); //search.Filter = String.Format("(SAMAccountName={FAY ImprovmentForm})", userName); //LdapSearcher.Filter = string.Format("(&(objectClass=user)(department={0}))", Department); //search.Filter = string.Format("(&(objectClass=group)(SAMAccountName=" + Department + "))", userName); search.PropertiesToLoad.Add("cn"); search.PropertiesToLoad.Add("samaccountname"); search.PropertiesToLoad.Add("givenname"); search.PropertiesToLoad.Add("sn"); search.PropertiesToLoad.Add("mail"); SearchResult result = search.FindOne(); string samaccountname = (string)result.Properties["samaccountname"][0]; string givenname = (string)result.Properties["givenname"][0]; string surname = (string)result.Properties["sn"][0]; string email = (string)result.Properties["mail"][0]; if (result == null) { return "User doesn't Exist"; } else { return "Emails sent successfully!"; }

    A 1 Reply Last reply
    0
    • S Slow Learner

      Hi, I have a need to send out emails to all the people who are in a given group say "HR Department". I need to be able to get all the emails through a for loop and then as it retrieves I should send emails to them. Can some one please help me with the code how to get the emails out of AD. this is what I came up with but not sure how to go about writing a for loop to get all the emails in the given group. I am having difficulty to send both the loginName and the group that I am interested in to the AD in the search.Filter. public static string SendEmails(string loginName) { string Department = "HR Department" string userName = ExtractUserName(loginName); DirectorySearcher search = new DirectorySearcher(); search.Filter = String.Format("(SAMAccountName={0})", userName); //search.Filter = String.Format("(SAMAccountName={FAY ImprovmentForm})", userName); //LdapSearcher.Filter = string.Format("(&(objectClass=user)(department={0}))", Department); //search.Filter = string.Format("(&(objectClass=group)(SAMAccountName=" + Department + "))", userName); search.PropertiesToLoad.Add("cn"); search.PropertiesToLoad.Add("samaccountname"); search.PropertiesToLoad.Add("givenname"); search.PropertiesToLoad.Add("sn"); search.PropertiesToLoad.Add("mail"); SearchResult result = search.FindOne(); string samaccountname = (string)result.Properties["samaccountname"][0]; string givenname = (string)result.Properties["givenname"][0]; string surname = (string)result.Properties["sn"][0]; string email = (string)result.Properties["mail"][0]; if (result == null) { return "User doesn't Exist"; } else { return "Emails sent successfully!"; }

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      This question has nothing to do with ASP.NET. Please read this article, Howto: (Almost) Everything In Active Directory via C#[^] This covered almost everything realted with AD.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups