Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

272
Views
Get email addresses from distribution list using c#

What is the best way to get all the individual email addresses comprising an exchange distribution list?

For eg: I have this distribution list called abc@domainname.com that has email addresses:

  1. a@domainname.com
  2. b@domainname.com
  3. c@domainname.com

Now I need to get these addresses using C# code.

I found solution using LDAP but I felt it'd be a hassle to figure out LDAP path to my Active Directory.

// How do I get this LDAP Path, username and password?  
// Is the username and password service account credentials of the app?  
// And do they need to be registered in AD?  
var entry = new DirectoryEntry("LDAP Path");//, username, password);

UPDATE: For me, LDAP way only worked for finding email addresses inside AD groups, for eg: named ITSolutionDeliveryDevelopers group. NOT inside Exchange Distribution Lists, for eg: named abc@domainname.com.

// I was able to figure out entry as suggested by @Gabriel Luci and all of the following possible formats worked for me:
// ngroupnet.com is my company domain name.
var entry = new DirectoryEntry();
var entry = new DirectoryEntry("LDAP://ngroupnet.com");
var entry = new DirectoryEntry("LDAP://ngroupnet.com", "MyAccountUsername", "MyAccountPassword");
var entry = new DirectoryEntry("LDAP://ngroupnet.com", "MyName@mycompany.com", "MyEmailAccountPassword");

LDAP Way:

public static List<string> GetDistributionListMembers(string dlName = "abc@domainname.com")
{
    var result = new List<string>();
    try
    {
        // How do I get this LDAP Path?
        var entry = new DirectoryEntry("LDAP Path");//, username, password);
        var search = new DirectorySearcher(entry);
        search.Filter = $"CN={dlName}";
        int i = search.Filter.Length;

        string str = "", str1 = "";
        foreach (SearchResult AdObj in search.FindAll())
        {
            foreach (String objName in AdObj.GetDirectoryEntry().Properties["member"])
            {
                str += Convert.ToString(objName) + "&lt;Br>";
                int selIndex = objName.IndexOf("CN=") + 3;
                int selEnd = objName.IndexOf(",OU") - 3;
                str1 += objName.Substring(selIndex, selEnd).Replace("\\", "");

                DirectorySearcher dsSearch = new DirectorySearcher(entry);
                dsSearch.Filter = "CN=" + objName.Substring(selIndex, selEnd).Replace("\\", "");
                foreach (SearchResult rs in dsSearch.FindAll())
                {
                    //str1 += "&lt;p align='right'><font face='calibri' color='#2266aa' size=2>" + Convert.ToString(rs.GetDirectoryEntry().Properties["mail"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["displayName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["sAMAccountName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["department"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["memberOf"].Value) + "&lt;/font></p>";
                    str1 = Convert.ToString(rs.GetDirectoryEntry().Properties["mail"].Value);
                    result.Add(str1);
                }
            }

        }
        return result;
    }
    catch (Exception ex)
    {
        //Do some logging or what have you.
        throw;
    }
}

So I just went with the EWS route.

EWS Way:

public static static List<string> GetDistributionListMembers(string dlName = "abc@domainname.com")
{
    try
    {
        var service = new ExchangeService();
        var cred = new WebCredentials("sharedmailbox@domain.com", "some_password");
        service.Credentials = cred;
        service.Url = new Uri("https://outlook.office365.com/ews/exchange.asmx");
        service.TraceEnabled = true;
        service.TraceFlags = TraceFlags.All;

        var expandedEmailAddresses = new List<string>();

        ExpandGroupResults myGroupMembers = service.ExpandGroup(dlName);

        foreach (EmailAddress address in myGroupMembers.Members)
        {
            expandedEmailAddresses.Add(address.Address);
        }

       return expandedEmailAddresses;
    }
    catch (Exception ex)
    {
        // The DL doesn't have any members. Handle it how you want.
        // Handle/ Log other errors.
    }
}

Is EWS approach a good way?

If Yes, then I'm good. If not, I'll have to figure out that LDAP path.

Or if there's even a better way, please let me know.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

If the computer you run this from is joined to the same domain as the group you're looking for, then you don't need to figure out the LDAP path. You can just do:

var search = new DirectorySearcher();

If your computer is not joined to the same domain, then you just use the domain name:

var entry = new DirectoryEntry("LDAP://domainname.com");

This requires that there is no firewall blocking port 389 between your computer and the domain controller(s). If you need to pass credentials, then do that:

var entry = new DirectoryEntry("LDAP://domainname.com", username, password);

The credentials can be any user on the domain.

That said, there are a lot of inefficiencies in your code that will make it run much slower than needed. I wrote an article about this that can help you update your code: Active Directory: Better Performance

Is EWS approach a good way?

If it works, it works. I'm not an expert on EWS (although I have used it), but I'm fairly certain that's using Basic Authentication, which is going to be disabled in October.

over 4 years ago · Santiago Trujillo Report

0

If all the Mailboxes are on Office365 then i would suggest you use the Graph API instead eg https://docs.microsoft.com/en-us/graph/api/group-list-members?view=graph-rest-1.0&tabs=http . There are several advantage in terms of security eg you could use Application permissions and all you need is access to the directory while if you did the same thing in EWS it would require full access to at least one mailbox.

LDAP will be best performing of the 3 if ultimate speed in the only thing that is important.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!