Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Outlook Mail with C#

I am using the following code as I attempt to connect to my Outlook mail. Now, I must be doing something wrong because I try to get the inbox mails and I always get 0 mails (when this is not the case). This is my code

 Microsoft.Office.Interop.Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
 nameSpace.Logon("", "", Missing.Value, Missing.Value);

 inboxFolder = nameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
 Console.WriteLine("Folders: {0}", inboxFolder.Folders.Count);

I have several email accounts in my Outlook profile. When I write the following

Console.WriteLine("Accounts: {0}",nameSpace.Accounts.Count);
Console.WriteLine("Name: {0}", nameSpace.Accounts[1].DisplayName);

The total number of accounts is displayed correctly, and so is the name of the account i really want to access (index 1). Now, the problem is that I need to access a specific folder within that account. How do I do this?

like image 589
Soph Avatar asked Dec 03 '11 14:12

Soph


People also ask

How do I make my computer read my emails?

Turn Read Aloud on or off in OutlookSelect File > Options > Accessibility. Under Application display options, select or clear the Show Read Aloud checkbox.

How do I view the summary of an email in Outlook?

From the View menu, select Current View, and then click Customize Current View. Click Other Settings, and then click Preview unread items. Click OK, and then, to close the View Summary window, click OK again.

How do I open Outlook email?

Go to the Outlook.com sign-in page and select Sign in. Enter your email address or phone number and select Next. On the Enter password page, clear Keep me signed in. Enter your password and select Sign in.


1 Answers

I could solve this! It was quite easy actually. Here is how I could access the desired folder:

// [email protected] is the name of my account
// Unsent mails is the name of the folder I wanted to access
inboxFolder = nameSpace.Folders["[email protected]"].Folders["Unsent mails"];

foreach (Microsoft.Office.Interop.Outlook.MailItem mailItem in inboxFolder.Items)
{
    if (mailItem.UnRead) // I only process the mail if unread
    {
        Console.WriteLine("Accounts: {0}", mailItem.Body);
    }    
}
like image 168
Soph Avatar answered Sep 20 '22 17:09

Soph