无法转换 COM 对象 - Microsoft Outlook & C#
我编写了此代码来查看 Outlook 邮箱中的未读项目,代码如下:
Microsoft.Office.Interop.Outlook.Application app;
Microsoft.Office.Interop.Outlook.Items items;
Microsoft.Office.Interop.Outlook.NameSpace ns;
Microsoft.Office.Interop.Outlook.MAPIFolder inbox;
Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
app = application;
ns = application.Session;
inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
items = inbox.Items;
foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
{
if (mail.UnRead == true)
{
MessageBox.Show(mail.Subject.ToString());
}
}
但在 foreach 循环中我收到此错误:
“无法将类型“System.__ComObject”的 COM 对象转换为接口类型“Microsoft.Office.Interop.Outlook.MailItem”。此操作失败,因为对 IID 为“{00063034”的接口的 COM 组件上的 QueryInterface 调用-0000-0000-C000-000000000046}' 由于以下错误而失败:不支持此类接口(HRESULT 异常:0x80004002 (E_NOINTERFACE))。"
您能帮我解决这个错误吗?
I have written this code to view the unread items in my outlook mail box and here is the code:
Microsoft.Office.Interop.Outlook.Application app;
Microsoft.Office.Interop.Outlook.Items items;
Microsoft.Office.Interop.Outlook.NameSpace ns;
Microsoft.Office.Interop.Outlook.MAPIFolder inbox;
Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application();
app = application;
ns = application.Session;
inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
items = inbox.Items;
foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items)
{
if (mail.UnRead == true)
{
MessageBox.Show(mail.Subject.ToString());
}
}
but on the foreach loop I am getting this error:
"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))."
Can you please assist me how to resolve this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不久前我不得不解决像你这样的问题。
希望有帮助。
这里的问题是
_explorer.CurrentFolder.Items
可以包含比MailItem
更多的对象(PostItem
就是其中之一)。I had to get around something like your problem a while back.
Hope that helps.
The issue here is that
_explorer.CurrentFolder.Items
can contain more objects than justMailItem
(PostItem
being one of them).在检查其属性之前尝试检查该项目是否是有效的
mailitem
:try to check the item is a valid
mailitem
before checking its properties :当我测试它时,以下代码运行良好。但我必须提到,我参考的是“Microsoft Outlook 14.0 对象库”。您碰巧使用其他版本吗?
请注意,在 Outlook 中,许多项目都有一些共同的属性(例如过期时间),因此您可以作为绝望的解决方法,使用“动态”数据类型 - 要么作为未知项目类型的后备方案,要么作为默认值(只要因为你对性能下降感到满意)。
The following code worked fine when I tested it. But I must mention that my reference was to "Microsoft Outlook 14.0 Object Library". Do you happen to use another version?
Please note that in Outlook, many items have some common properties (e.g. expiration time), so you can, as a desperate workaround, use a "dynamic" datatype - either as a fallback scenario for unknown item types or as your default (as long as you're fine with the performance hit).
好的!稍微调整了解决方案,这对我来说效果很好
Nice! adapted the solution a bit, this worked well for me
我遇到了这个问题,因为我的 Outlook 文件夹中有一封电子邮件,该电子邮件是发送失败的电子邮件。
请确保从文件夹中删除此类邮件,然后重试。对我来说,从文件夹中删除电子邮件无需对代码进行任何更改。
I ran into this problem because I had an email in my outlook folder which was delivery failed email.
Make sure you remove such mails from your folder and try again. For me, removing the email from the folder worked without making any changes in the code.