Error thrown when opening e-mails using outlook api

别等时光非礼了梦想. 提交于 2019-12-25 13:14:11

问题


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;

回答1:


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.



来源:https://stackoverflow.com/questions/6583134/error-thrown-when-opening-e-mails-using-outlook-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!