c# - EWS 获取所有文件夹中未读邮件的数量

标签 c# winforms .net-3.5 exchangewebservices

我正在尝试从 Exchange 获取特定用户的未读电子邮件数。

我可以像这样从收件箱中获取电子邮件数量:

SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
ItemView view = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view);
int unreadCount = 0;
foreach (EmailMessage i in findResults)
    {
        unreadCount++;
    }
label1.Text = unreadCount.ToString();

效果很好。
我还能够获取收件箱中的所有子文件夹:

FindFoldersResults findResults1 = service.FindFolders(
    WellKnownFolderName.Inbox,
    new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep });

foreach (Folder folder in findResults1.Folders)
{
    Console.WriteLine(folder.DisplayName);
}

问题是我无法将这两者结合在一起。
我知道我可以进行嵌套的 foreach 循环,但我想避免这种情况。

我发现了这些问题:Exchange Web Services (EWS) FindItems within All Folders , 但至少需要使用 Outlook 2010 才能创建 AllItems 文件夹。

我知道我可以创建 SearchFilterCollection,但是如何向它添加规则以便它搜索收件箱和所有子文件夹中的未读电子邮件?

编辑:

这是我到目前为止尝试做的:

private int getEmailCount()
{
    int unreadCount = 0;

    FolderView viewFolders = new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep, PropertySet = new PropertySet(BasePropertySet.IdOnly) };
    ItemView viewEmails = new ItemView(int.MaxValue) { PropertySet = new PropertySet(BasePropertySet.IdOnly) };
    SearchFilter unreadFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, unreadFilter, viewEmails);
    unreadCount += findResults.Count();

    FindFoldersResults inboxFolders = service.FindFolders(WellKnownFolderName.Inbox, viewFolders);

    foreach (Folder folder in inboxFolders.Folders)
    {
        findResults = service.FindItems(folder.Id, unreadFilter, viewEmails);
        unreadCount += findResults.Count();
    }

    return unreadCount;
    }

基本上这是可行的,但是当我创建了多个子文件夹时,它开始运行得非常慢。
我可以执行一次以获得相同的结果而不是多次查询吗?

最佳答案

我搜索了一下并创建了这个函数:

    public void getEmailCount(Action<int> callback)
    {
        int unreadCount = 0;

        FolderView viewFolders = new FolderView(int.MaxValue) { Traversal = FolderTraversal.Deep, PropertySet = new PropertySet(BasePropertySet.IdOnly) };
        ItemView viewEmails = new ItemView(int.MaxValue) { PropertySet = new PropertySet(BasePropertySet.IdOnly) };
        SearchFilter unreadFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
        SearchFilter folderFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "AllItems"));

        FindFoldersResults inboxFolders = service.FindFolders(WellKnownFolderName.Root, folderFilter, viewFolders);

        if (inboxFolders.Count() == 0)//if we don't have AllItems folder
        {
            //search all items in Inbox and subfolders
            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, unreadFilter, viewEmails);
            unreadCount += findResults.Count();

            inboxFolders = service.FindFolders(WellKnownFolderName.Inbox, viewFolders);
            foreach (Folder folder in inboxFolders.Folders)
            {
                findResults = service.FindItems(folder.Id, unreadFilter, viewEmails);
                unreadCount += findResults.Count();
            }
        }
        else //AllItems is avilable
        {
            foreach (Folder folder in inboxFolders.Folders)
            {
                FindItemsResults<Item> findResults = service.FindItems(folder.Id, unreadFilter, viewEmails);
                unreadCount += findResults.Count();
            }
        }

        callback(unreadCount);
    }

基本上它会检查我们是否有可用的 AllItems 文件夹。
如果 YES,那么我们执行一个简单的查询,返回所有未读消息。
如果 NO 那么我们循环收件箱中的所有文件夹。这会比较慢,并且取决于我们有多少文件夹和级别。

欢迎任何修复和改进:)

关于c# - EWS 获取所有文件夹中未读邮件的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12687401/

相关文章:

c# - 防止下拉区域在 Windows 窗体中打开组合框控件

c# - 有没有办法在运行时从控件中删除 Alt 字符快捷方式?

c# - 使用 C# 在通用列表中查找最大整数?

c# - HttpWebRequest 没有关闭方法?

c# - "direct"虚拟调用与 C# 中的接口(interface)调用的性能

c# - 使用 Oracle 命令参数的问题

c# - 如何检测单元格值更改datagridview c#

c# - WebApi Controller 没有找到 Controller 的操作

c# - 更改 ComboBox 边框轮廓颜色

c# 数组函数参数传递一维