Count unread emails from multiple outlook mailboxes using EWS c#

浪子不回头ぞ 提交于 2019-12-13 05:59:38

问题


I want to count unread inbox counts from multiple outlook mailboxes (distribution lists) using exchange web services with C#. But the code keeps counting my inbox unread emails and not from the mailboxes I have provided in the code.

Below is my code,

protected void Page_Load(object sender, EventArgs e)
{
   getunreademailcount_valid();
   getunreademailcount_invalid();
}
public void getunreademailcount_valid()
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);
    service.AutodiscoverUrl("validdocs@abc.com");
    Mailbox mb = new Mailbox("validdocs@abc.com");
    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.Inbox,
    new FolderView(int.MaxValue));

    if (inboxFolders.Count() == 0)
    { 
        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();
            Response.Write(folder.DisplayName + " : " + findResults.Count() + "<br /><br />");
        }
     }
     else 
     {
         foreach (Folder folder in inboxFolders.Folders)
         {
             FindItemsResults<Item> findResults = service.FindItems(folder.Id, unreadFilter, viewEmails);
             Response.Write(folder.DisplayName + " : " + findResults.Count() + "<br /><br />");
         }
     }
}
public void getunreademailcount_invalid()
{
    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);

    service.AutodiscoverUrl("invaliddocs@abc.com");
    Mailbox mb = new Mailbox("invaliddocs@abc.com");
   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.Inbox,
    new FolderView(int.MaxValue));

    if (inboxFolders.Count() == 0)
    {
        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();
             Response.Write(folder.DisplayName + " : " + findResults.Count() + "<br />");
         }
     }
     else 
     {
         foreach (Folder folder in inboxFolders.Folders)
         {
            FindItemsResults<Item> findResults = service.FindItems(folder.Id, unreadFilter, viewEmails);
            Response.Write(folder.DisplayName + " : " + findResults.Count() + "<br />");
         }
     }
}

What am I doing wrong here?


回答1:


You need to use the FolderId overload to specify the Mailbox you want to access otherwise the Mailbox that belongs to the credentials you are using. You code doesn't make much sense given what you are trying to do eg if all you want to do is access the UnRead Message count on a folder you can use UnreadCount on that folder you don't need to use FindItem eg

        Mailbox MailboxYouWantToAccess = new Mailbox("mailbox@yourdomain.com");
        FolderId InboxFolderId = new FolderId(WellKnownFolderName.Inbox, MailboxYouWantToAccess);
        Folder InboxFolder = Folder.Bind(service, InboxFolderId);
        Console.WriteLine(InboxFolder.UnreadCount);

If you want to use the AllItems folder (that would only be their if the Outlook Desktop client is being used then you can use)

         SearchFilter AllItemsSF = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "AllItems");
        ExtendedPropertyDefinition PR_FOLDER_TYPE = new ExtendedPropertyDefinition(13825, MapiPropertyType.Integer);
        SearchFilter SearchFoldersOnly = new SearchFilter.IsEqualTo(PR_FOLDER_TYPE, 2);
        SearchFilter sfCol = new SearchFilter.SearchFilterCollection(LogicalOperator.And) { AllItemsSF, SearchFoldersOnly };
        FolderId SearchRootId = new FolderId(WellKnownFolderName.Root, MailboxYouWantToAccess);
        FolderView fvFolderView = new FolderView(100);
        fvFolderView.Traversal = FolderTraversal.Deep;
        FindFoldersResults ffResults = service.FindFolders(SearchRootId, sfCol, fvFolderView);
        if (ffResults.Folders.Count == 1)
        {
            Console.WriteLine(ffResults.Folders[0].UnreadCount);
        }


来源:https://stackoverflow.com/questions/33020084/count-unread-emails-from-multiple-outlook-mailboxes-using-ews-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!