简体   繁体   English

使用EWS c#计算来自多个Outlook邮箱的未读电子邮件

[英]Count unread emails from multiple outlook mailboxes using EWS c#

I want to count unread inbox counts from multiple outlook mailboxes (distribution lists) using exchange web services with C#. 我想使用C#使用Exchange Web服务来计算来自多个Outlook邮箱(通讯组列表)的未读收件箱计数。 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? 我在这里做错了什么?

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. 您需要使用FolderId重载来指定要访问的邮箱,否则就属于您正在使用的凭据的邮箱。 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 鉴于您要尝试执行的操作,您的代码没有多大意义,例如,如果您要做的只是访问文件夹中的未读邮件计数,则可以在该文件夹上使用UnreadCount,而无需使用FindItem。

        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) 如果要使用AllItems文件夹(仅在使用Outlook Desktop客户端时才是它们),则可以使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM