How to loop through all MailItems of certain Outlook subfolders

淺唱寂寞╮ 提交于 2019-12-04 12:47:48
John

While the above code may work it is likely that you will come across an unhandled InvalidCastException as not all items in the root folder will be mail items (e.g. meeting requests).
The following code worked for me:

foreach (object item in items)
{
    if (item is Outlook.MailItem)
    {
        ///The rest of your code
    }
}
Zeeshan Sadiq

// iterating backwards is needed because of .move below

for (int i = theRootFolder.Items.Count; i > 0; i--)
{
     Outlook.MailItem mi = (Outlook.MailItem)theRootFolder.Items[i];
     if (mi != null)
     {
         if (!mi.Subject.StartsWith("M1"))
         {
             mi.Move(_TRIM_archiveFolder);
         }
     }
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!