Want to enumerate Outlook folders

前端 未结 2 1948
孤独总比滥情好
孤独总比滥情好 2021-01-19 04:05

I\'m looking for some code (C# or VB.NET preferred) to iterate through all folders in an Outlook mailbox and return the names of those folders. I\'m not looking to pop up t

2条回答
  •  北海茫月
    2021-01-19 04:49

    I prefer a more LINQ friendly approach:

    private IEnumerable GetAllFolders(Folders folders)
    {
        foreach (MAPIFolder f in folders) {
            yield return f;
            foreach (var subfolder in GetAllFolders(f.Folders)) {
                yield return subfolder;
            }
        }
    }
    

    Then you can peruse the folders any way like. For example:

    private IEnumerable GetAllEmail(NameSpace ns)
    {
        foreach (var f in GetAllFolders(ns.Folders)) {
            if (f == DELETE_FOLDER) continue;
            if (f.DefaultItemType == OlItemType.olMailItem) {
                // Party!
            }
        }
    }
    

提交回复
热议问题