如何遍历每个文件夹中的每个邮件项,Outlook加载项2010

时间:2014-02-27 11:47:25

标签: c# office-interop outlook-addin

这是我到目前为止所做的,(不工作)

private void ScanAllMailItems()
{
    Outlook.Folders folders = _outlookNameSpace.Folders;
    foreach (Outlook.Folder folder in folders)
    {
        foreach (Outlook.MailItem m in folder.Items)
        {
            ScanMailBody(m);
        }
    }
}

没有例外,似乎folder.items总是空的。 Outlook邮箱本身有10个文件夹,每个文件夹有20-300个邮件

1 个答案:

答案 0 :(得分:2)

解决这样的问题,感谢Simon的链接。

    private void ScanAllMailItems()
    {
        var theRootFolder = (Outlook.Folder)_outlookNameSpace.DefaultStore.GetRootFolder();
        RecurseThroughFolders(theRootFolder, 0);
    }

    private void RecurseThroughFolders(Outlook.Folder theRootFolder, int depth)
    {
        if (theRootFolder.DefaultItemType != Outlook.OlItemType.olMailItem)
            return;

        foreach (object item in theRootFolder.Items)
        {
            var mailItem = item as Outlook.MailItem;
            if (mailItem != null)
            {
                var mi = mailItem;
                ScanMailBody(mi);
            }
        }

        foreach (Outlook.Folder folder in theRootFolder.Folders)
        {
            RecurseThroughFolders(folder, depth + 1);
        }
    }