E Zhang
E Zhang

Reputation: 171

How to read emails in Outlook

I tried to read emails from an email Outlook account in a .pst/.ost file. The code is as follows.

Outlook.Application myApp = new Outlook.Application();
Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");

// Get the email account root folder
Outlook.MAPIFolder myEmail = mapiNameSpace.Folders[textBox_EmailBody_Account.Text.Trim()];

// Get the specific subfolder
Outlook.MAPIFolder emailFolder = (Outlook.MAPIFolder)myEmail.Folders[textBox_EmailBody_Folder.Text.Trim()];

It works fine for a .pst file, but fails for a .ost file (myEmail == null). The .pst file is in the folder "C:\Users\name\Documents\Outlook Files" and the .ost file is in "C:\Users\name\AppData\Local\Microsoft\Outlook". I need help on this. Thanks.

Upvotes: 0

Views: 34

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66286

It means the top folder name does not match what you pass to mapiNameSpace.Folders[]. Look at the value of textBox_EmailBody_Account.Text and compare it to the folders names in the mapiNameSpace.Folders collection.

Upvotes: 1

Related Questions