Trabalhando com Listas de Distribuição no Exchange Server

Trabalhando com Listas de Distribuição

A API Aspose.Email fornece a capacidade de criar e ler listas de distribuição do servidor Exchange. Listas de distribuição podem ser criadas no servidor e membros podem ser adicionados a elas usando o IEWSClient. Este artigo mostra como trabalhar com listas de distribuição no servidor Exchange.

Criando Lista de Distribuição

O seguinte código mostra como criar uma lista de distribuição.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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);

Buscar Lista de Distribuição Privada

O seguinte código mostra como buscar uma lista de distribuição privada.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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);
}
}

Criar MailAddress a partir do Id da Lista de Distribuição

O seguinte código mostra como criar um MailAddress a partir do Id da lista de distribuição.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
ExchangeDistributionList[] distributionLists = client.ListDistributionLists();
String id = distributionLists[0].Id;
MailAddress distributionListAddress = new MailAddress("privateDL", true);
distributionListAddress.Id.EWSId = id;

Expandir Lista de Distribuição Pública

O seguinte código mostra como expandir a lista de distribuição pública.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
MailAddressCollection members = client.ExpandDistributionList(new MailAddress("public.distribution.list@host.com"));
foreach (MailAddress member in members)
{
Console.WriteLine(member.Address);
}

Adicionando membros

Adicionando membros à Lista de Distribuição Privada

O seguinte código mostra como adicionar membros a uma lista de distribuição privada.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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);

Adicionar membros sem listar

O seguinte código mostra como adicionar membros sem listar.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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);

Enviar para Lista de Distribuição Privada

O seguinte código mostra como enviar uma mensagem para uma lista de distribuição privada.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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);

Deletando membros

Deletando membros da Lista de Distribuição Privada

O seguinte código mostra como deletar membros de uma lista de distribuição privada.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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);

Deletar membros sem listar

O seguinte código mostra como deletar membros sem listar.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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 membersToDelete = new MailAddressCollection();
MailAddress addressToDelete = new MailAddress("address", true);
//addressToDelete.Id.EWSId = "member's id";
membersToDelete.Add(addressToDelete);
client.AddToDistributionList(distributionList, membersToDelete);

Deletar Lista de Distribuição Privada

O seguinte código mostra como deletar uma lista de distribuição privada.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList[] distributionLists = client.ListDistributionLists();
client.DeleteDistributionList(distributionLists[0],true);

Deletar sem Listar

O seguinte código mostra como deletar sem listar.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
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);