微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法投射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循环中,我得到这个错误

“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是其中之一).

原文地址:https://www.jb51.cc/windows/365465.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐