Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 8, 2022 09:58
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/32ba9a85096bf605d5f239a882cf057b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/32ba9a85096bf605d5f239a882cf057b to your computer and use it in GitHub Desktop.
Add and Remove Members in MS Exchange Distribution Lists in Java
// Connect to Exchange Server
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// Get lists
ExchangeDistributionList[] distributionLists = client.listDistributionLists();
// Create a new list and add members to be added
MailAddressCollection newMembers = new MailAddressCollection();
newMembers.add("address4@host.com");
newMembers.add("address5@host.com");
// Add new list to existing 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 lists
ExchangeDistributionList[] distributionLists = client.listDistributionLists();
// Fetch members of desired list
MailAddressCollection members = client.fetchDistributionList(distributionLists[0]);
// Create a new list and add members to be deleted
MailAddressCollection membersToDelete = new MailAddressCollection();
membersToDelete.addMailAddress(members.get_Item(0));
membersToDelete.addMailAddress(members.get_Item(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