0
votes

I'm trying to list all folders (in order to later on get messages from one specific folder) using the Microsoft Office Interops.

If I run this code with storeIdx set to 1 (which is my personal mailbox) it works great, but if I try running this on storeIdx 2 (which is a shared mailbox I also use) it returns absolutely nothing:

    private void ProcessMail()
    {
        Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
        _NameSpace ns = app.GetNamespace("MAPI");
        int storeIdx = 2;

        MAPIFolder inbox = ns.Stores[storeIdx].GetDefaultFolder(OlDefaultFolders.olFolderInbox);

        Console.WriteLine("Getting folders from mailbox: " + ns.Stores[storeIdx].DisplayName + " in folder " + inbox.Name +"\r\n");
        Console.WriteLine("FOLDERS:\r\n");

        foreach (MAPIFolder f in inbox.Folders)
        {
            Console.WriteLine(f.Name + " (" + f.FullFolderPath + ")");
        }
    }

I've tried a couple of different things after reading on various sites (MSDN among others) but it simply won't show me any subfolders in the inbox of the shared mailbox. In Outlook I'm able to see them just fine and work with them.

The mailboxes are all on the same Exchange server.

Any help would be appreciated.

2

2 Answers

0
votes

You can get all Folders like this:

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
List<Folder> folders = new List<Folder>();
foreach(Folder f in app.Session.Folders)
{
    if(ff.DefaultMessageClass.Contains("IPM.Post")
        folders.Add(f);
    folders.AddRange(GetSubFolders(f));
    // Dont forget to release the object cause Outlook don't like too many open obejcts
    Marshal.ReleaseComObject(f);
}

GetSubFolders:

public List<Folder> GetSubFolders(Folder f)
{
    List<Folder> folders = new List<Folder>();
    if(f.Folders.Count > 0)
    {
        foreach(Folder ff in f.Folders)
        {
            if(ff.DefaultMessageClass.Contains("IPM.Post")
                folders.Add(ff);
            folders.AddRange(GetSubFolders(ff));
            Marshal.ReleasComObject(ff);
        }
    }
    return folders;
}
0
votes

I think that the collection Stores of GetNamespace obj representing all the stores available in the current profile(current session). You could use the NameSpace.GetSharedDefaultFolder to get a shared mailbox.

Reference link:Get list of all Outlook folders and subfolders