如何在最近的N个Outlook.Items上应用过滤器并在过滤后获取Outlook.Items?

时间:2017-07-25 16:52:20

标签: email outlook outlook-filter

我想使用以下代码过滤掉未读的Outlook邮件,但我想在最近的N个Outlook.Items.But上应用此限制我找不到任何此类方法。

Outlook.Explorer currExplorer = null;
Outlook.Folder currFolder = null;
Outlook.Items folderItems = null;
Outlook.Items restrictedItems = null;
Outlook.MailItem mail = null;
Outlook.Attachments attachments = null;
int attachmentCount = 0;

try
{

    currExplorer = OutlookApp.ActiveExplorer();
    currFolder = currExplorer.CurrentFolder as Outlook.Folder;
    if (currFolder.DefaultItemType == Outlook.OlItemType.olMailItem)
    {
        folderItems = currFolder.Items;
        restrictedItems = folderItems.Restrict("[Unread]=true");


        for (int i = 1; i <= restrictedItems.Count; i++)
        {
            mail = restrictedItems[i] as Outlook.MailItem;
            if (mail != null)
            {
                attachments = mail.Attachments;
                attachmentCount += attachments.Count;
                Marshal.ReleaseComObject(attachments);
                Marshal.ReleaseComObject(mail);
            }
        }

    }

}

1 个答案:

答案 0 :(得分:0)

您需要使用Restrict方法两次 - 首先获取最近的N个项目,然后使用第二个来获取它们之间的未读项目。没有查询来获取集合中的N个项目,但您可以使用时间和时间范围来过滤项目,有关详细信息,请参阅Enumerating, Searching, and Filtering Items in a Folder

您可能会发现以下文章有用: