从Outlook中的非WellKnownFolder检索电子邮件

时间:2018-12-07 10:29:39

标签: c# outlook exchangewebservices

我正在使用Microsoft.Exchange.WebServices从Outlook Exchange收件箱中提取所有电子邮件,并且下面的代码很完美

ExchangeService service = EmailCredentials();
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(CountRec));

foreach (Item i in findResults.Items)
{
  countOfEmails++;
}

现在,请求从收件箱外部的另一个用户创建的文件夹中提取电子邮件。我发现的所有内容都使用MAPI或EAGetMail,但我需要使用交换Web服务。这可能吗?

**编辑

感谢@farbiondrive使用他的代码进行了一些调整,我现在可以使用

ExchangeService service = EmailCredentials();

 // Return only folders that contain items.
 SearchFilter searchFilter = new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0);

 FolderView view = new FolderView(10);
 // Unlike FindItem searches, folder searches can be deep traversals.

 view.Traversal = FolderTraversal.Deep;
 // Send the request to search the mailbox and get the results.

FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.MsgFolderRoot, searchFilter, view);

foreach (Folder folder in findFolderResults.Folders)
{
    if (folder.DisplayName == "MyFolder")
    {
        FindItemsResults<Item> findResults = service.FindItems(folder.Id, new ItemView(CountRec));

        foreach (Item i in findResults.Items)
        {
            countOfEmails++;
        }
    }
 }

1 个答案:

答案 0 :(得分:3)

很遗憾,我无法对其进行测试,但是您可以尝试一下吗?

ExchangeService service = EmailCredentials();

// Return only folders that contain items.
SearchFilter searchFilter = new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0);

// Unlike FindItem searches, folder searches can be deep traversals.
view.Traversal = FolderTraversal.Deep;

// Send the request to search the mailbox and get the results.
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Root, searchFilter, view);

foreach (var folder in findFolderResults.Folders)
{
    FindItemsResults<Item> findResults = service.FindItems(folder, new ItemView(CountRec));

    foreach (Item i in findResults.Items)
    {
        countOfEmails++;
    }
}