【发布时间】:2021-09-23 22:35:39
【问题描述】:
运行此循环以删除多个相同的电子邮件时,我遇到了此异常。我认为这与 oItems 的长度有关,由于项目被删除而丢失 1,但它没有在 foreach 循环中更新。
//Get the Inbox folder.
Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
//Get the Items collection in the Inbox folder.
Outlook.Items oItems = oInbox.Items;
//loops through all inbox items
foreach (object item in oItems)
{
//checks to only use MailItems (no MeetingItems etc.)
if (item is Outlook.MailItem)
{
if (item.Subject != null)
{
if (item.SenderEmailAddress == "no-reply@virginpulse.com")
{
item.Delete();
}
}
}
}
【问题讨论】:
-
从循环中的集合中删除项目时,从长度 1 循环到 0 总是安全的。
-
请记住,OOM 中的所有集合都是基于 1,而不是 0。
标签: c# outlook comexception