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

时间:2015-10-08 15:31:17

标签: c# outlook exchangewebservices

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

以下是我的代码,

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

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

您需要使用FolderId重载来指定要访问的邮箱,否则指定属于您正在使用的凭据的邮箱。考虑到你想要做的事情,你的代码没有多大意义,例如,如果你想要做的只是访问文件夹上的UnRead消息计数,你可以在该文件夹上使用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);

如果您想使用AllItems文件夹(只有在使用Outlook桌面客户端时才能使用它们,那么您可以使用)

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