![](/img/trans.png)
[英]System.OutOfMemoryException' was thrown when using Tcp Sockets
[英]System.OutOfMemoryException thrown when gathering emails using Exchange Web Server API
我的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);
有人告訴我錯誤位於
emails.Add((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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.