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\DELETEDI 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;
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.
精彩评论