EWS获取未读邮件数的所有文件夹文件夹、邮件、EWS

由网友(流鸢ぶ)分享简介:我试图让未读邮件数从Exchange针对特定用户。I'm trying to get number of unread emails from Exchange for specific user.我能够得到的电子邮件数量从收件箱中,像这样:I'm able to get number of emails fro...

我试图让未读邮件数从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.

我发现了以下问题: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 :)

阅读全文

相关推荐

最新文章