I am creating a C# console application in Visual Studio 2015 that prints to the console all emails. I am having problems when I try to create the MAPIFolder object. I used the code from this post: Read emails from non default accounts in Outlook. I can create a MAPIFolder object from the default account using the namespace, but I can’t create any folder object using the Stores.
using Microsoft.Office.Interop.Outlook;
using static System.Console;
namespace MoveEmailsDriver
{
class ProcessEmails
{
static void Main(string[] args)
{
PrintEmailBody();
}
public static void PrintEmailBody()
{
Application app = new Application();
_NameSpace ns = app.GetNamespace("MAPI");
Stores stores = ns.Stores;
foreach(Store store in stores)
{
MAPIFolder inboxFolder = store.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
foreach(MailItem item in inboxFolder.Items)
{
WriteLine(item.Body);
}
}
}
}
}
I figured it out. I had to create a MAPIFolder object with the GetRootFolder() method. This is the updated code: