3
votes

I am trying to simply read and print the contents of emails from a specific folder and another inbox in Outlook using C# (not my default inbox). I'm finding it difficult to find examples of this on the web and have failed on my own. I know how to print the emails of the default account as well, just not additional ones.

My code here simply iterates over a list of all the inboxes and prints their names out. The one I want to read is the first element in the collection. I appreciate any help with this issue. Thanks.

using System;
using System.Collections;
using Microsoft.Office.Interop.Outlook;

public class StorageReplies {

    public static void Main() {
        Application app = new Microsoft.Office.Interop.Outlook.Application(); 
        _NameSpace ns = app.GetNamespace("MAPI");

        Folders folders = ns.Folders;

        foreach(MAPIFolder f in folders) {
            Console.WriteLine(f.Name);
        }
    }
}
1

1 Answers

1
votes

You could obtain the Store for each folder, and then call GetDefaultFolder method to obtain the inbox folder for the corresponding store like this:

foreach (MAPIFolder f in folders)
{
    MAPIFolder inbox_folder = f.Store.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

    foreach (MailItem item in inbox_folder.Items)
    {
        //Access item here
    }
}

However, instead of doing that, it makes sense to loop through the Stores property directly like this:

Stores stores = ns.Stores;

foreach (Store store in stores)
{
    MAPIFolder inbox_folder = store.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

    foreach (MailItem item in inbox_folder.Items)
    {
        //Access item here
    }                
}