簡體   English   中英

C# MS Exchange 將電子郵件移至文件夾

[英]C# MS Exchange Move Email To Folder

補充:感謝用戶@grapkulec,我正在使用

using Microsoft.Exchange.WebServices.Data;

我正在嘗試將電子郵件移動到我已經在 Outlook 中創建的文件夾(使用 MS Exchange)。 到目前為止,我已經能夠將電子郵件移至草稿或其他眾所周知的文件夾名稱,但無法成功將其移至我創建的名為“示例”的文件夾。

foreach (Item email in findResults.Items)
email.Move(WellKnownFolderName.Drafts);

上面的代碼有效; 但我不想使用眾所周知的文件夾。 而且,如果我嘗試將代碼更改為:

email.Move(Folder.(Example));

要么

email.Move(Folder.["Example"]);

它不會移動(在這兩種情況下,都會引發錯誤)。 我在 MSDN、SO 和一般 C# 上找到了大量關於如何將電子郵件移動到文件夾中的示例 - 但僅限於 Outlook“眾所周知”的文件夾(草稿、垃圾郵件等),它不適用於我創建的文件夾。

解決了!

無論多次嘗試, Move命令都失敗了,因為 ID 格式錯誤。 顯然,移動操作不允許使用名稱。 我曾嘗試將DisplayName作為標識符,這就是讓我失望的原因。 最后,我放棄了DisplayName ,這會有所幫助。 相反,我通過將它存儲在一個變量中來指向 ID(它停止了煩人的“ID 格式錯誤”錯誤),並且移動成功了。

代碼:

Folder rootfolder = Folder.Bind(service, WellKnownFolderName.MsgFolderRoot);
rootfolder.Load();

foreach (Folder folder in rootfolder.FindFolders(new FolderView(100)))
{
    // Finds the emails in a certain folder, in this case the Junk Email
    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.JunkEmail, new ItemView(10));

    // This IF limits what folder the program will seek
    if (folder.DisplayName == "Example")
    {
        // Trust me, the ID is a pain if you want to manually copy and paste it. This stores it in a variable
        var fid = folder.Id;
        Console.WriteLine(fid);
        foreach (Item item in findResults.Items)
        {
            // Load the email, move the email into the id.  Note that MOVE needs a valid ID, which is why storing the ID in a variable works easily.
            item.Load();
            item.Move(fid);
        }
    }
}

看來您正在使用 EWS 托管 API,所以這是我如何做這些事情的答案。

項目上的移動方法可以接受 WellKnownFolderName 或文件夾 ID。 如果我理解正確,您想將您的電子郵件移動到名為“示例”的文件夾中。 所以首先你需要為這個文件夾獲取文件夾對象:

var filter = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "Example");
var view = new FolderView(1)
{
    PropertySet = new PropertySet(BasePropertySet.FirstClassProperties)
};
var findFoldersResults = exService.FindFolders(filter, view);
folder = findFoldersResults.FirstOrDefault(f => f.DisplayName.Equals("Example", StringComparison.OrdinalIgnoreCase));

現在你應該有你的“示例”文件夾變量,你可以將它的 id 傳遞給電子郵件的 Move 方法。 有關更多詳細信息,請查看有關如何使用 EWS 托管 API 的 msdn 頁面,那里有很多簡單和基本的使用示例。

順便說一句:WellKnownFolderNames 枚舉是最常見的 Exchange 文件夾(如收件箱、已發送郵件等)的便利類型。您必須通過搜索/或綁定自行檢索的任何其他內容,以防萬一其他 Exchange 對象。

基於這些答案,創建了一種移動到文件夾的工作方法,可能對某人有用:

/// <summary>
/// Moves the email to the specified folder.
/// </summary>
/// <param name="mail">Email message to move.</param>
/// <param name="folderName">Display name of the folder.</param>
public void MoveToFolder(EmailMessage mail, string folderName)
{
    Folder rootfolder = Folder.Bind(_exchangeService, WellKnownFolderName.MsgFolderRoot);
    rootfolder.Load();
    Folder foundFolder = rootfolder.FindFolders(new FolderView(100)).FirstOrDefault(x => x.DisplayName == folderName);
    if (foundFolder == default(Folder))
    {
        throw new DirectoryNotFoundException(string.Format("Could not find folder {0}.", folderName));
    }

    mail.Move(foundFolder.Id);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM