使用C#计算Outlook中特定日期的邮件数

时间:2014-12-01 11:59:28

标签: c# outlook

我正在编写一个工具来将Outlook邮件移动到Excel,其中我使用了两个日期选择器,一个开始日期,另一个结束日期。我不知道如何形成一个循环,循环特定日期,然后跳转到下一个。

for (DateTime j = fd; j <= td; j=j.AddDays(1))
{

                    var outlookXcell = ((Microsoft.Office.Interop.Outlook.MailItem) myInbox.Items[fd]);
                    MessageBox.Show(outlookXcell.SenderName);
                    var mailSub = outlookXcell.Subject;

                    var mailcontent = outlookXcell.Body;

                    var senderemailid = outlookXcell.SenderEmailAddress;
}

3 个答案:

答案 0 :(得分:1)

正如Eugene所提到的,Items.Find / FindNext / Restrict将完成这项工作。您需要的查询类似于以下内容:([ReceivedTime] > '10/01/2014') AND ([ReceivedTime] < '12/01/2014'

答案 1 :(得分:0)

您可以使用Find / FindNext或Restrict方法获取符合您需求的项目集合。例如,请参阅How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)

答案 2 :(得分:0)

Atlast做R&amp; D,我想解决。如果你觉得有助于优化代码工作更好。添加评论。

int mCount = myInbox.Items.Count;
     for (int a = 1; a < mCount; a++)

            {
               var itemn = (Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[a];

                if (itemn.CreationTime >= fd && itemn.CreationTime < td)
                {
                    string subject = itemn.Subject;
                    DateTime reciv = itemn.CreationTime;
                    var mContent = itemn.Body;
                    var senderId = itemn.SenderEmailAddress;

                    worksheet.Cells[xlrow, 2] = reciv;
                    worksheet.Cells[xlrow, 3] = senderId;
                    worksheet.Cells[xlrow, 4] = subject + worksheet.Cells.WrapText;
                    worksheet.Cells[xlrow, 5] = mContent + worksheet.Cells.WrapText;
                    worksheet.Cells.WrapText = true;
                    xlrow++;
                }

            }