Working with Distribution Lists on Exchange Server

Working with Distribution Lists

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.

Creating a Distribution List

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
System::SharedPtr<ExchangeDistributionList> distributionList = System::MakeObject<ExchangeDistributionList>();
distributionList->set_DisplayName(u"test private list");
System::SharedPtr<MailAddressCollection> members = System::MakeObject<MailAddressCollection>();
members->Add(u"address1@host.com");
members->Add(u"address2@host.com");
members->Add(u"address3@host.com");
try
{
client->CreateDistributionList(distributionList, members);
}
catch (System::Exception& ex)
{
System::Console::WriteLine(ex.get_Message());
}

Fetch Private Distribution List

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::ArrayPtr<System::SharedPtr<ExchangeDistributionList>> distributionLists = client->ListDistributionLists();
for (System::SharedPtr<ExchangeDistributionList> distributionList : distributionLists)
{
System::SharedPtr<MailAddressCollection> members = client->FetchDistributionList(distributionList);
for (auto member : System::IterateOver(members))
{
System::Console::WriteLine(member->get_Address());
}
}

Expand Public Distribution List

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<MailAddressCollection> members = client->ExpandDistributionList(System::MakeObject<MailAddress>(u"public.distribution.list@host.com"));
for (auto member : System::IterateOver(members))
{
System::Console::WriteLine(member->get_Address());
}

Adding members

Adding members to a Private Distribution List

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
System::ArrayPtr<System::SharedPtr<ExchangeDistributionList>> distributionLists = client->ListDistributionLists();
System::SharedPtr<MailAddressCollection> newMembers = System::MakeObject<MailAddressCollection>();
newMembers->Add(u"address4@host.com");
newMembers->Add(u"address5@host.com");
client->AddToDistributionList(distributionLists[0], newMembers);

Add members without listing

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
System::SharedPtr<ExchangeDistributionList> distributionList = System::MakeObject<ExchangeDistributionList>();
distributionList->set_Id(u"list's id");
distributionList->set_ChangeKey(u"list's change key");
System::SharedPtr<MailAddressCollection> newMembers = System::MakeObject<MailAddressCollection>();
newMembers->Add(u"address6@host.com");
client->AddToDistributionList(distributionList, newMembers);

Send Email to Private Distribution List

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::ArrayPtr<System::SharedPtr<ExchangeDistributionList>> distributionLists = client->ListDistributionLists();
System::SharedPtr<MailAddress> distributionListAddress = distributionLists[0]->ToMailAddress();
System::SharedPtr<MailMessage> message = System::MakeObject<MailMessage>(System::MakeObject<MailAddress>(u"from@host.com"), distributionListAddress);
message->set_Subject(u"sendToPrivateDistributionList");
client->Send(message);

Deleting members

Deleting members from Private Distribution List

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::ArrayPtr<System::SharedPtr<ExchangeDistributionList>> distributionLists = client->ListDistributionLists();
System::SharedPtr<MailAddressCollection> members = client->FetchDistributionList(distributionLists[0]);
System::SharedPtr<MailAddressCollection> membersToDelete = System::MakeObject<MailAddressCollection>();
membersToDelete->Add(members->idx_get(0));
membersToDelete->Add(members->idx_get(1));
client->DeleteFromDistributionList(distributionLists[0], membersToDelete);

Delete members without listing

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
System::SharedPtr<ExchangeDistributionList> distributionList = System::MakeObject<ExchangeDistributionList>();
distributionList->set_Id(u"list's id");
distributionList->set_ChangeKey(u"list's change key");
System::SharedPtr<MailAddressCollection> membersToDelete = System::MakeObject<MailAddressCollection>();
System::SharedPtr<MailAddress> addressToDelete = System::MakeObject<MailAddress>(u"address", true);
//addressToDelete.Id.EWSId = "member's id";
membersToDelete->Add(addressToDelete);
try
{
client->AddToDistributionList(distributionList, membersToDelete);
}
catch (System::Exception& ex)
{
System::Console::WriteLine(ex.get_Message());
}

Delete Private Distribution List

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
try
{
System::ArrayPtr<System::SharedPtr<ExchangeDistributionList>> distributionLists = client->ListDistributionLists();
client->DeleteDistributionList(distributionLists[0], true);
}
catch (System::Exception& ex)
{
System::Console::WriteLine(ex.get_Message());
}

Delete without Listing

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

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-C
System::SharedPtr<IEWSClient> client = GetExchangeEWSClient(GetExchangeTestUser());
System::SharedPtr<ExchangeDistributionList> distributionList = System::MakeObject<ExchangeDistributionList>();
distributionList->set_Id(u"list's id");
try
{
client->DeleteDistributionList(distributionList, true);
}
catch (System::Exception& ex)
{
System::Console::WriteLine(ex.get_Message());
}