使用EWS C#计算来自多个Outlook邮箱的未读电子邮件 [英] Count unread emails from multiple outlook mailboxes using EWS c#

查看:124
本文介绍了使用EWS C#计算来自多个Outlook邮箱的未读电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有C#的Exchange Web服务来计数来自多个Outlook邮箱(通讯组列表)的未读收件箱计数. 但是代码会继续计数 收件箱未读电子邮件,而不是我在代码中提供的邮箱中.

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.

下面是我的代码,

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

我在做什么错了?

推荐答案

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

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

如果要使用AllItems文件夹(只有在使用Outlook Desktop客户端的情况下才可以使用),则可以使用

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

这篇关于使用EWS C#计算来自多个Outlook邮箱的未读电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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