C# 使用Exchange Web Server API收集电子邮件时引发System.OutOfMemoryException

C# 使用Exchange Web Server API收集电子邮件时引发System.OutOfMemoryException,c#,windows,exception,service,exchangewebservices,C#,Windows,Exception,Service,Exchangewebservices,我的windows服务源代码中出现以下错误: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.Collections.Generic.List`1.set_Capacity(Int32 value) at System.Collections.Generic.List`1.EnsureCapacity(Int32 min) at System

我的windows服务源代码中出现以下错误:

System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' 
was thrown.
at System.Collections.Generic.List`1.set_Capacity(Int32 value) at 
System.Collections.Generic.List`1.EnsureCapacity(Int32 min) at 
System.Collections.Generic.List`1.Add(T item)
当我尝试使用Exchange Web Server API从收件箱加载电子邮件时。以下是用于验证和读取邮箱的代码:

ExchangeService service = new 
ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials("name@domain.co.uk", "*****");
service.TraceEnabled = true;
service.TraceFlags = TraceFlags.All;
service.AutodiscoverUrl("name@domain.co.uk", RedirectionUrlValidationCallBack);

//retrieve first 50 emails
int offset = 0;
int pageSize = 50;
bool moreEmails = true;
ItemView view = new ItemView(pageSize, offset, OffsetBasePoint.Beginning);

view.PropertySet = PropertySet.IdOnly;
FindItemsResults<Item> findResults;
List<EmailMessage> emails = new List<EmailMessage>();
findResults = service.FindItems(WellKnownFolderName.Inbox, view);

while (moreEmails)
{
    foreach (var item in findResults.Items)
    {
        emails.Add((EmailMessage)item);
    }

    moreEmails = findResults.MoreAvailable;

    if (moreEmails)
    {
        view.Offset += pageSize;
    }
}

 PropertySet properties = (BasePropertySet.FirstClassProperties);
 service.LoadPropertiesForItems(emails, properties);
ExchangeService服务=新建
ExchangeService(ExchangeVersion.Exchange2013\u SP1);
service.Credentials=新的WebCredentials(“name@domain.co.uk", "*****");
service.TraceEnabled=true;
service.TraceFlags=TraceFlags.All;
服务。自动发现URL(“name@domain.co.uk,重定向UrlValidationCallback);
//检索前50封电子邮件
整数偏移=0;
int pageSize=50;
bool=true;
ItemView视图=新的ItemView(页面大小、偏移量、偏移量基点.开始);
view.PropertySet=PropertySet.IdOnly;
FindItemsResults findResults;
列表电子邮件=新列表();
findResults=service.FindItems(WellKnownFolderName.Inbox,视图);
while(更多电子邮件)
{
foreach(findResults.Items中的变量项)
{
电子邮件。添加((EmailMessage)项);
}
moreEmails=findResults.MoreAvailable;
如果(更多电子邮件)
{
view.Offset+=页面大小;
}
}
PropertySet属性=(BasePropertySet.FirstClassProperties);
服务。加载项目的属性(电子邮件、属性);
我被告知错误位于

电子邮件。添加((EmailMessage)项)


但我不确定如何着手解决这个错误。非常感谢您的帮助。

在while循环中滑动对FindItems的调用。没有它,您只需将相同的项目添加到集合中,直到内存耗尽

while (moreEmails)
{
    findResults = service.FindItems(WellKnownFolderName.Inbox, view);

    foreach (var item in findResults.Items)
    {
        emails.Add((EmailMessage)item);
    }

    moreEmails = findResults.MoreAvailable;

    if (moreEmails)
    {
        view.Offset += pageSize;
    }
}

那么,当它爆炸时,你已经在邮件列表中添加了多少封邮件?@Polyfun邮件列表是空的。此外,邮箱中只有49封电子邮件,这是最大的邮件数。邮件的运行时类型是什么?是否可以转换为EmailMessage?看起来您从未更新过“findResults”,因此,如果第一批包含更多内容,它将始终具有更多可用内容。虽然如果你只有49个,那么我认为它应该是假的。@CSmith谢谢,我更新了循环中的findResults,它工作了!