C#EWS将在收件箱中找到的所有电子邮件保存到项目列表中

时间:2019-08-20 08:17:34

标签: c# exchangewebservices

我当前的代码访问Inbox中所有未读的电子邮件。但是,因为这是一种方法,它将返回电子邮件列表以对每个电子邮件进行进一步处理。

我不确定如何将找到的电子邮件分组到列表或表格中。

任何建议将不胜感激。

private static GetNewEmailInInbox(ExchangeService service, int batch, string autoDiscoverURL)
{
    if (service != null)
    {
        Console.WriteLine("Accessing system account mailbox...");

        TimeSpan ts = new TimeSpan(0, -1, 0, 0);
        DateTime date = DateTime.Now.Add(ts);
        service.AutodiscoverUrl(autoDiscoverURL);
        SearchFilter sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

        FindItemsResults<Item> emailItemList;
        ItemView view = new ItemView(50);
        int inboxCount = 1;
        do
        {
            emailItemList = service.FindItems(WellKnownFolderName.Inbox, sf, view);
            foreach (var emailItem in emailItemList.Items)
            {
                Console.WriteLine(inboxCount + ". " + emailItem.Subject);
                inboxCount++;

                //add this email to an allEmailList
            }

            if (!emailItemList.NextPageOffset.HasValue)
                break;
        }
        while (emailItemList.MoreAvailable);
    }
return allEmailList;
}

1 个答案:

答案 0 :(得分:0)

您可以使用IList Interface对电子邮件进行分组。

IList<T> allEmailList = new List<T>();

FindItemsResults<Item> emailItemList = service.FindItems(WellKnownFolderName.Inbox, sf, view);
foreach (var emailItem in emailItemList.Items.OfType<T>())
{
    Console.WriteLine(inboxCount + ". " + emailItem.Subject);
    inboxCount++;

    //add this email to an allEmailList
    allEmailList.Add(emailItem);
}