EWS获取未读邮件数的所有文件夹 [英] EWS get count of unread emails from all folders

查看:1454
本文介绍了EWS获取未读邮件数的所有文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让未读邮件数从Exchange针对特定用户。

I'm trying to get number of unread emails from Exchange for specific user.

我能够得到的电子邮件数量从收件箱中,像这样:

I'm able to get number of emails from Inbox like so:

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();

这伟大工程。
我也能得到所有子文件夹是收件箱:

This works great.
I'm also able to get all subfolders is Inbox:

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

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

问题是,我不能在这两个结合起来。
我知道,我能做的嵌套的foreach循环,但我想避免这种情况。

Problem is that I'm not able to combine these two together.
I know that I can do nested foreach loop, but I would like to avoid that.

我发现了以下问题:<一href="http://stackoverflow.com/questions/7401080/exchange-web-services-ews-finditems-within-all-folders">Exchange在所有文件夹的Web服务(EWS)FindItems 的,但它需要至少使用Outlook 2010,以创建 AllItems 文件夹。

I found these question: Exchange Web Services (EWS) FindItems within All Folders, but it requires to at least use Outlook 2010 in order to create AllItems folder.

我知道,我可以创建 SearchFilterCollection ,但如何将规则添加到它,这样它会搜索收件箱中的未读邮件和所有子文件夹?

I know that I can create SearchFilterCollection, but how to add rules to it so that it will search for unread emails in Inbox and all subfolders?

编辑:

这就是我试图做为止:

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;
    }

基本上这个工作,但是当我创建了多个子文件夹就开始工作非常缓慢。
而不是多个查询我可以做一个得到同样的结果?

Basically this works, but when I have created multiple subfolders it started to work very slow.
Instead of multiple queries can I do one to get same results?

推荐答案

我已经搜索了一下,创造了这个功能:

I've searched a bit and created this function:

    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 文件夹即时拍摄。
如果然后我们返回所有未读邮件的,简单的查询。
如果 NO 然后我们循环里面所有的收件箱文件夹中。这是慢,取决于我们有多少个文件夹和层次都有。

Basically it checks if we have AllItems folder avilable.
If YES then we do one, simple query that returns all unread messages.
If NO then we loop all folders inside Inbox. This is slower and depends on how many folders and levels we have.

任何修正和改进,欢迎:)

Any fixes and improvements are welcome :)

这篇关于EWS获取未读邮件数的所有文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆