在C#中,您可以使用Exchange Web Services (EWS) 庫來管理Exchange郵件服務器上的郵件文件夾。以下是一些基本操作:
添加引用:首先,您需要在項目中添加對Microsoft.Exchange.WebServices.dll的引用。這個DLL包含在Exchange Server SDK中,您可以從Microsoft官方網站下載。
創建ExchangeService對象:使用Microsoft.Exchange.WebServices.Data命名空間中的ExchangeService類來連接到Exchange服務器。
using Microsoft.Exchange.WebServices.Data;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); // 根據您的Exchange版本進行選擇
service.Credentials = new WebCredentials("UserName", "Password", "Domain");
service.AutodiscoverUrl("UserPrincipalName", RedirectionUrlValidationCallback);
FolderId folderId = new FolderId(WellKnownFolderName.Inbox, "UserPrincipalName");
Folder folder = Folder.Bind(service, folderId);
Folder newFolder = new Folder(service);
newFolder.DisplayName = "New Folder";
newFolder.Save(WellKnownFolderName.Inbox);
folder.Delete(DeleteMode.HardDelete);
folder.DisplayName = "Updated Folder Name";
folder.Update();
// 移動文件夾
folder.Move(new FolderId(WellKnownFolderName.DeletedItems));
// 復制文件夾
Folder copyFolder = folder.Copy(new FolderId(WellKnownFolderName.Drafts));
FindFoldersResults findFoldersResults = folder.FindFolders(new FolderView(100));
foreach (Folder subFolder in findFoldersResults)
{
Console.WriteLine(subFolder.DisplayName);
}
這只是Exchange EWS庫提供的功能的一小部分。您可以查看官方文檔以獲取更多信息和示例代碼:https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications