Reading Outlook 2007 mails from .net application




this subject seemed to be so simple (thanks to the PIA interface) but
actually trying it is causing me some headaches. I am writing a .net
application which should (on demand) scan the mailbox infolder for mails with
a certain subject text. A few lines of code seemed to do the magic -- but
fail to execute for a few mails (reproducable).

Here is my (simplified) code:

using System;
using Microsoft.Office.Interop.Outlook; // c:\Program Files\Microsoft
Visual Studio 9.0\Visual Studio Tools for

namespace OutlookTest
class Program
private static Microsoft.Office.Interop.Outlook.Application
pOutlookInstance = null;

static void Main(string[] args)
int iItem;
Microsoft.Office.Interop.Outlook.MailItem CurrentMail;

pOutlookInstance = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MAPIFolder MailFolder =
Microsoft.Office.Interop.Outlook.Items FolderItemList = MailFolder.Items;

Console.WriteLine("Outlook[InBox] contains " +
FolderItemList.Count.ToString() + " mail items.");

for (iItem = 1; iItem <= FolderItemList.Count; ++iItem)
CurrentMail =
Console.WriteLine("#" + iItem.ToString() + ": Date=" +
CurrentMail.SentOn.ToString("yyyy-MM-dd") + "; Sender=" +

As said before, the code works well but stops with an InvalidCast exception
and an error code HRESULT: 0x80004002 (E_NOINTERFACE) for several mail items.
Looking at these mail items in the inbox I do not see any problems; the mails
seem(to me) not to be different in structure or type.

I guess I am missing something but I do not find out how I can make my above
example work for all mail items in the inbox. Any idea is appreciated. Thanks
in advance.



Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question