Exchange获取邮箱文件及子文件下未读邮件数量

    /// 
    /// 获取邮箱文件及子文件下未读邮件数量
    /// 
    /// 
    /// 
    public int getEmailCount(ExchangeService service)
    {
        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);

        //search all items in Inbox and subfolders
        FindItemsResults 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();
        }

        return unreadCount;
    }

 

你可能感兴趣的:(日常记录)