在 C# 中從 MS Exchange 分發列表中添加和刪除成員

在 .NET 應用程序中使用 MS Exchange Server 時,您可能需要使用分發列表。在 上一篇文章 中,您了解瞭如何創建和獲取 Exchange 通訊組列表。在本文中,您將了解如何使用 C# 在 Exchange 通訊組列表中添加或刪除成員。

用於添加和刪除 Exchange 分發列表成員的 C# API

為了與 MS Exchange Server 上的分發列表成員一起工作,我們將使用 Aspose.Email for .NET。它是一個眾所周知的 API,用於創建電子郵件客戶端應用程序並與 MS Exchange Server 一起工作。您可以 下載 API 的 DLL 或使用以下命令從 NuGet 安裝它。

PM> Install-Package Aspose.Email

在 C# 中將成員添加到 MS Exchange 分發列表

以下是在 C# .NET 中將成員添加到 MS Exchange 分發列表的步驟。

以下代碼示例顯示如何使用 C# 將成員添加到 Exchange 通訊組列表。

// 連接到 Exchange 服務器
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);

C# 從 MS Exchange 分發列表中刪除成員

以下是使用 C# .NET 從 MS Exchange 分發列表中刪除成員的步驟。

以下代碼示例顯示如何使用 C# 從 Exchange 通訊組列表中刪除成員。

// 連接到 Exchange 服務器
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);

用於 Exchange 分發列表的 C# API - 獲取免費許可證

您可以獲得免費的臨時許可證,以在沒有評估限制的情況下使用 Aspose.Email for .NET。

結論

在本文中,您了解瞭如何在 Microsoft Exchange Server 上管理通訊組列表中的成員。特別是,您已經了解瞭如何在 C# 中以編程方式在 Exchange 通訊組列表中添加或刪除成員。同時,您可以瀏覽 文檔 以閱讀有關 Aspose.Email for .NET 的更多信息。此外,您可以通過我們的 論壇 提問。

也可以看看