Trying to get Inox Mail Items from Outlook in C#

Posted on 2006-06-05
Last Modified: 2008-02-01

I am trying to get some information about items in my Inbox using Visual Studio 2005.  However I am getting the following error in the Foreach loop. (code below)

System.InvalidCastException was unhandled
  Message="Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."
       at GetMailConsole.Program.AccessOutlook() in C:\Documents and Settings\mike.kenyon\My Documents\Visual Studio 2005\Projects\GetMailConsole\GetMailConsole\Program.cs:line 36
       at GetMailConsole.Program.Main(String[] args) in C:\Documents and Settings\mike.kenyon\My Documents\Visual Studio 2005\Projects\GetMailConsole\GetMailConsole\Program.cs:line 21
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

The Code is :

               Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
               Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;
               Microsoft.Office.Interop.Outlook.PostItem item = null;

                Outlook.Application app = new Outlook.ApplicationClass();
                Outlook.NameSpace NS = app.GetNamespace("MAPI");
                inboxFolder = NS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

                subFolder = inboxFolder.Folders["Accounts"];
                Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
                Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());

                foreach (Outlook.MailItem t in subFolder.Items)

I basically don't really understand what the error message means (COM is not my strong point!).  If anyone knows thats great.  I think the code is correct to access MS Outlook.

Thanks in anticipation of any help.

Question by:hydev
    LVL 28

    Accepted Solution

    that looked weird, since it should have worked so I done a little sarching and found:
    more precisely last comment:
    "Just in case this helps anyone else, I had to change the loop to check to
    make sure the items in the inbox were mailitems before I casted them.
    Appointment reminders and such were blowing it up."
    so .. there's your problem :)

    Author Comment


    You are a genius!

    Thank you very much.  It appears you do have to check that is a mailitem!

    LVL 28

    Expert Comment

    well, I didn't know about it either, just got lucky on my google search ;)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now