c# - Exchange Web 服务 (EWS) 在所有文件夹中查找项目

标签 c# exchange-server exchangewebservices

我正在使用以下代码查找用户发送的所有电子邮件,但这只搜索主收件箱文件夹,不检查任何子文件夹。我想搜索所有邮件项目,包括任何子文件夹。

我已经尝试过 WellKnownFolderName.RootWellKnownFolderName.Inbox,它们只搜索那些文件夹,而不是子文件夹。

private static void SearchItems(string email)
{
    ItemView iv = new ItemView(10);
    FindItemsResults<Item> fiitems = _service.FindItems(WellKnownFolderName.Inbox, "from:username@example.com", iv);

    foreach (Item item in fiitems)
    {
        Console.WriteLine("Subject:\t" + item.Subject);
        Console.WriteLine("Received At:\t\t" + item.DateTimeReceived.ToString("dd MMMM yyyy"));
        Console.WriteLine();
    }

    Console.WriteLine("Press Enter to continue");
    Console.ReadLine();
}

最佳答案

我在 Exchange 中的 AllItems 文件夹中找到了一些信息 over at Glen's blog .我已将 PowerShell 脚本移植到 C#,如下所示。

private static void SearchItems()
{
    ExtendedPropertyDefinition allFoldersType = 
        new ExtendedPropertyDefinition(13825, MapiPropertyType.Integer);

    FolderId rootFolderId = new FolderId(WellKnownFolderName.Root);
    FolderView folderView = new FolderView(1000);
    folderView.Traversal = FolderTraversal.Shallow;

    SearchFilter searchFilter1 = new SearchFilter.IsEqualTo(allFoldersType, "2");
    SearchFilter searchFilter2 = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "allitems");

    SearchFilter.SearchFilterCollection searchFilterCollection = 
        new SearchFilter.SearchFilterCollection(LogicalOperator.And);
    searchFilterCollection.Add(searchFilter1);
    searchFilterCollection.Add(searchFilter2);

    FindFoldersResults findFoldersResults = 
        _service.FindFolders(rootFolderId, searchFilterCollection, folderView);

    if (findFoldersResults.Folders.Count > 0)
    {
        Folder allItemsFolder = findFoldersResults.Folders[0];
        Console.WriteLine("Folder:\t" + allItemsFolder.DisplayName);

        ItemView iv = new ItemView(1000);
        FindItemsResults<Item> findResults = 
            allItemsFolder.FindItems("System.Message.DateReceived:01/01/2011..01/31/2011", iv);

        foreach (Item item in findResults)
        {
            Console.WriteLine("Subject:\t" + item.Subject);
            Console.WriteLine("Received At:\t\t" + item.DateTimeReceived.ToString("dd MMMM yyyy"));
            Console.WriteLine("Is New:\t\t" + item.IsNew.ToString());
            Console.WriteLine("Has Attachments:\t\t" + item.HasAttachments.ToString());
            Console.WriteLine();
        }
    }

    Console.WriteLine("Press Enter to continue");
    Console.ReadLine();
}

关于c# - Exchange Web 服务 (EWS) 在所有文件夹中查找项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7401080/

相关文章:

c# - 删除对象序列中的连续重复项

c# - 自动发现本地异常 : The Autodiscover service couldn't be located

c# - 新的或修改的日历事件的 EWS PullSubscription

java - EWS 和 Exchange 2010 流通知 - 看到双重

c# - Linq 返回一个从列表中选择的新对象

c# - 如何使 CalendarExtender StartDate 属性采用当前日期?

java - 如何获取项目的 mimetype?

powershell - 无法在 Exchange 管理控制台中将变量与 Get-User -Filter 一起使用

email - 来自 WebSite 的 EWS 使用内部网的用户凭据和 Windows 身份验证

c# - 当你等待一个失败的任务时会发生什么