Praca z listami dystrybucyjnymi na serwerze Exchange
Praca z listami dystrybucyjnymi
Aspose.Email API zapewnia możliwość tworzenia i odczytywania list dystrybucyjnych z serwera Exchange. Listy dystrybucyjne mogą być tworzone na serwerze, a także można do nich dodawać członków przy użyciu IEWSClient. Ten artykuł pokazuje, jak pracować z listami dystrybucyjnymi na serwerze Exchange.
Tworzenie listy dystrybucyjnej
Poniższy fragment kodu pokazuje, jak utworzyć listę dystrybucyjną.
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList distributionList = new ExchangeDistributionList();
distributionList.setDisplayName("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);
Pobierz prywatną listę dystrybucyjną
Poniższy fragment kodu pokazuje, jak pobrać prywatną listę dystrybucyjną.
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList[] distributionLists = client.listDistributionLists();
for (ExchangeDistributionList distributionList : distributionLists) {
MailAddressCollection members = client.fetchDistributionList(distributionList);
for (MailAddress member : (Iterable<MailAddress>) members) {
System.out.println(member.getAddress());
}
}
Rozszerz publiczną listę dystrybucyjną
Poniższy fragment kodu pokazuje, jak rozszerzyć publiczną listę dystrybucyjną.
MailAddressCollection members = client.expandDistributionList(new MailAddress("public.distribution.list@host.com"));
for (MailAddress member : (Iterable<MailAddress>) members) {
System.out.println(member.getAddress());
}
Dodawanie członków
Dodawanie członków do prywatnej listy dystrybucyjnej
Poniższy fragment kodu pokazuje, jak dodać członków do prywatnej listy dystrybucyjnej.
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);
Dodawanie członków bez wymieniania
Poniższy fragment kodu pokazuje, jak dodać członków bez wymieniania.
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList distributionList = new ExchangeDistributionList();
distributionList.setId("list's id");
distributionList.setChangeKey("list's change key");
MailAddressCollection newMembers = new MailAddressCollection();
newMembers.add("address6@host.com");
client.addToDistributionList(distributionList, newMembers);
Wyślij do prywatnej listy dystrybucyjnej
Poniższy fragment kodu pokazuje, jak wysłać wiadomość do prywatnej listy dystrybucyjnej.
ExchangeDistributionList[] distributionLists = client.listDistributionLists();
MailAddress distributionListAddress = distributionLists[0].toMailAddress();
MailMessage message = new MailMessage(new MailAddress("from@host.com"), distributionListAddress);
message.setSubject("sendToPrivateDistributionList");
client.send(message);
Usuwanie członków
Usuwanie członków z prywatnej listy dystrybucyjnej
Poniższy fragment kodu pokazuje, jak usunąć członków z prywatnej listy dystrybucyjnej.
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.addMailAddress(members.get_Item(0));
membersToDelete.addMailAddress(members.get_Item(1));
client.deleteFromDistributionList(distributionLists[0], membersToDelete);
Usuwanie członków bez wymieniania
Poniższy fragment kodu pokazuje, jak usunąć członków bez wymieniania.
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList distributionList = new ExchangeDistributionList();
distributionList.setId("list's id");
distributionList.setChangeKey("list's change key");
MailAddressCollection membersToDelete = new MailAddressCollection();
MailAddress addressToDelete = new MailAddress("address", true);
// addressToDelete.Id.EWSId = "member's id";
membersToDelete.addMailAddress(addressToDelete);
client.addToDistributionList(distributionList, membersToDelete);
Usuń prywatną listę dystrybucyjną
Poniższy fragment kodu pokazuje, jak usunąć prywatną listę dystrybucyjną.
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList[] distributionLists = client.listDistributionLists();
client.deleteDistributionList(distributionLists[0], true);
Usuwanie bez wymieniania
Poniższy fragment kodu pokazuje, jak usunąć bez wyświetlania listy.
IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
ExchangeDistributionList distributionList = new ExchangeDistributionList();
distributionList.setId("list's id");
client.deleteDistributionList(distributionList, true);