Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 8, 2022 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/d2e7147cd939cf4f09e6d5f5626d1b38 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/d2e7147cd939cf4f09e6d5f5626d1b38 to your computer and use it in GitHub Desktop.
Add and Remove Members from MS Exchange Distribution Lists in C#
// Connect to Exchange Server
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Get all distribution lists
ExchangeDistributionList[] distributionLists = client.ListDistributionLists();
// Create a new list and add members
MailAddressCollection newMembers = new MailAddressCollection();
newMembers.Add("address4@host.com");
newMembers.Add("address5@host.com");
// Merge members to list
client.AddToDistributionList(distributionLists[0], newMembers);
// Connect to Exchange Server
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Get all distribution lists
ExchangeDistributionList[] distributionLists = client.ListDistributionLists();
// Fetch members from a specific list
MailAddressCollection members = client.FetchDistributionList(distributionLists[0]);
// Create a new list and add members to be deleted
MailAddressCollection membersToDelete = new MailAddressCollection();
membersToDelete.Add(members[0]);
membersToDelete.Add(members[1]);
// Delete members
client.DeleteFromDistributionList(distributionLists[0], membersToDelete);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment