最近电子邮件的 Outlook 分析计数

Outlook analytics count of recent email

一个简单的需求->统计最近7天收到的邮件数量。代码看起来很简单:

    void CountEmail()
        {
            Outlook.Explorer currExplorer = null;
            Outlook.Folder folder = null;
            Outlook.Items items = null;
            Outlook.ContactItem contact = null;
            string contactList = string.Empty;
            try
            {
                currExplorer = Application.ActiveExplorer();
                folder = (Outlook.Folder)currExplorer.CurrentFolder;

                items = folder.Items; 

                int count = items.Count; // 10082 count

                IEnumerable<Outlook.MailItem> mail = folder.Items.OfType<Outlook.MailItem>().Where(m => m.ReceivedTime <= DateTime.Now - new TimeSpan(7, 0, 0, 0)).Select(m => m);

                int itemscount = mail.Count<Outlook.MailItem>(); // out of memory here
.....

mail = ... 中的查询显然是延迟加载,因为没有时间花在这项工作上。后面的调用就是执行了,我运行出来还是内存。我不想要实际内容,只想要计数。

那么,如何获取最近7天内收到的邮件数量呢?

永远不要将 LINQ 与 COM 一起使用 - 它在您的代码中看起来很酷,但从性能的角度来看却很糟糕。更不用说在线模式下你可以运行脱离RPC通道

使用Items.Find/FindNextItems.Restrict