Error thrown when opening e-mails using outlook api

0

Question: I get the following error message when opening what appear to be valid .MSG files using Outlook API to open emails:

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)).

The MSG files open fine in outlook and I'm assured that the forms are standard outlook mail forms.

What is the most likely cause of this error?


Background: One of my colleagues has exported a large number of mailboxes from exchange as PSTs and then run a tool against the PST files to produce a file-structure resembling the original mailbox filled with .MSG files. For example:

\USERNAME
\USERNAME\INBOX
\USERNAME\SENT
\USERNAME\DRAFTS
\USERNAME\DELETED

I then scan these folders for .MSG files and attempt to open them using Outlook API.

Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();

Outlook.NameSpace ns = app.GetNamespace("MAPI");

Outlook.MailItem item = (Outlook.MailItem)ns.OpenSharedItem(EmailPath);

Subject = item.Subject;
c#
outlook
office-interop
outlook-2007
asked on Stack Overflow Jul 5, 2011 by Rich • edited Jun 24, 2020 by Martijn Pieters

1 Answer

0

All *Item objects in OOM implement the Type property. Retrieve that property dynamically using reflection instead of casting the returned object to MailItem.

OK use the "as" operator to test for the interface you are interested in without raising an exception.


User contributions licensed under CC BY-SA 3.0