• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1359
  • Last Modified:

Trying to get Inox Mail Items from Outlook in C#

Hi,

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))."
  Source="GetMailConsole"
  StackTrace:
       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)
                {
                    Console.WriteLine(t.Subject);
                }

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.

Mike
0
hydev
Asked:
hydev
  • 2
1 Solution
 
2266180Commented:
that looked weird, since it should have worked so I done a little sarching and found:
http://www.devnewsgroups.net/group/microsoft.public.office.developer.outlook.vba/topic11414.aspx
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 :)
0
 
hydevAuthor Commented:
ciuly,

You are a genius!

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

mike
0
 
2266180Commented:
well, I didn't know about it either, just got lucky on my google search ;)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now