加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

无法投射COM对象 – Microsoft Outlook和C#

发布时间:2020-12-13 22:44:11 所属栏目:Windows 来源:网络整理
导读:我写了这个代码来查看我的Outlook邮箱中未读的项目,这里是代码: Microsoft.Office.Interop.Outlook.Application app; Microsoft.Office.Interop.Outlook.Items items; Microsoft.Office.Interop.Outlook.NameSpace ns; Microsoft.Office.Interop.Outlook.MA
我写了这个代码来查看我的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循环中,我得到这个错误:

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

你能协助我如何解决这个错误?

我不得不绕过像你这样的问题一样.
foreach (Object _obj in _explorer.CurrentFolder.Items)
        {
            if (_obj is MailItem)
            {
                 MyMailHandler((MailItem)_obj);
            }
        }

希望有帮助.

这里的问题是_explorer.CurrentFolder.Items可以包含更多的对象,而不仅仅是MailItem(PostItem是其中之一).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读