How to Create, Manage, and Use Distribution Lists in Exchange Server

Aspose.Email API provides the capability to create and read distributions lists from the Exchange server. Distribution lists can be created on the server as well as members can be added to it using the IEWSClient. This article shows how to work with distribution lists on the Exchange server.

Create Distribution Lists

The following code snippet shows you how to create a distribution list.

IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList distributionList = new ExchangeDistributionList();
distributionList.DisplayName = "test private list";
MailAddressCollection members = new MailAddressCollection();
members.Add("address1@host.com");
members.Add("address2@host.com");
members.Add("address3@host.com");
client.CreateDistributionList(distributionList, members);

Fetch Private Distribution Lists

The following code snippet shows you how to fetch a private distribution list.

IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList[] distributionLists = client.ListDistributionLists();
foreach (ExchangeDistributionList distributionList in distributionLists)
{
    MailAddressCollection members = client.FetchDistributionList(distributionList);
    foreach (MailAddress member in members)
    {
        Console.WriteLine(member.Address);
    }
}

Create MailAddress from List ID

The following code snippet shows you how to create MailAddress from distribution list Id.


ExchangeDistributionList[] distributionLists = client.ListDistributionLists();
String id = distributionLists[0].Id;
MailAddress distributionListAddress = new MailAddress("privateDL", true);
distributionListAddress.Id.EWSId = id;

Expand Public Distribution Lists

The following code snippet shows you how to expand the public distribution List.


                MailAddressCollection members = client.ExpandDistributionList(new MailAddress("public.distribution.list@host.com"));
                foreach (MailAddress member in members)
                {
                    Console.WriteLine(member.Address);
                }

Add Members

Add Members to Private Lists

The following code snippet shows you how to add members to a private distribution list.

IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList[] distributionLists = client.ListDistributionLists();
MailAddressCollection newMembers = new MailAddressCollection();
newMembers.Add("address4@host.com");
newMembers.Add("address5@host.com");
client.AddToDistributionList(distributionLists[0], newMembers);

Add Members without Listing

The following code snippet shows you how to add members without listing.

IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList distributionList = new ExchangeDistributionList();
distributionList.Id = "list's id";
distributionList.ChangeKey = "list's change key";
MailAddressCollection newMembers = new MailAddressCollection();
newMembers.Add("address6@host.com");
client.AddToDistributionList(distributionList, newMembers);

Send to Private Distribution List

The following code snippet shows you how to send a message to a private distribution list.


                ExchangeDistributionList[] distributionLists = client.ListDistributionLists();
                MailAddress distributionListAddress = distributionLists[0].ToMailAddress();
                MailMessage message = new MailMessage(new MailAddress("from@host.com"), distributionListAddress);
                message.Subject = "sendToPrivateDistributionList";
                client.Send(message);

Delete Members

Delete Members from Private Lists

The following code snippet shows you how to delete members from a private distribution list.

IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList[] distributionLists = client.ListDistributionLists();
MailAddressCollection members = client.FetchDistributionList(distributionLists[0]);
MailAddressCollection membersToDelete = new MailAddressCollection();
membersToDelete.Add(members[0]);
membersToDelete.Add(members[1]);
client.DeleteFromDistributionList(distributionLists[0], membersToDelete);

Delete Members without Listing

The following code snippet shows you how to delete members without listing.

IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList distributionList = new ExchangeDistributionList();
distributionList.Id = "list's id";
client.DeleteDistributionList(distributionList,true);

Delete Private Distribution List

The following code snippet shows you how to delete a private distribution list.

IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList[] distributionLists = client.ListDistributionLists();
client.DeleteDistributionList(distributionLists[0],true);