无法转换 COM 对象 - Microsoft Outlook & C#

发布于 2024-10-11 13:16:31 字数 1109 浏览 5 评论 0原文

我编写了此代码来查看 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迎风吟唱 2024-10-18 13:16:31

不久前我不得不解决像你这样的问题。

        foreach (Object _obj in _explorer.CurrentFolder.Items)
        {
            if (_obj is MailItem)
            {
                 MyMailHandler((MailItem)_obj);
            }
        }

希望有帮助。

这里的问题是 _explorer.CurrentFolder.Items 可以包含比 MailItem 更多的对象(PostItem 就是其中之一)。

I had to get around something like your problem a while back.

        foreach (Object _obj in _explorer.CurrentFolder.Items)
        {
            if (_obj is MailItem)
            {
                 MyMailHandler((MailItem)_obj);
            }
        }

Hope that helps.

The issue here is that _explorer.CurrentFolder.Items can contain more objects than just MailItem (PostItem being one of them).

温暖的光 2024-10-18 13:16:31

在检查其属性之前尝试检查该项目是否是有效的mailitem

foreach (Object mail in items)
{
    if ((mail as Outlook.MailItem)!=null && (mail as Outlook.MailItem).UnRead == true)
    {
        MessageBox.Show((mail as Outlook.MailItem).Subject.ToString());
    }
}

try to check the item is a valid mailitem before checking its properties :

foreach (Object mail in items)
{
    if ((mail as Outlook.MailItem)!=null && (mail as Outlook.MailItem).UnRead == true)
    {
        MessageBox.Show((mail as Outlook.MailItem).Subject.ToString());
    }
}
俏︾媚 2024-10-18 13:16:31

当我测试它时,以下代码运行良好。但我必须提到,我参考的是“Microsoft Outlook 14.0 对象库”。您碰巧使用其他版本吗?

    public class Outlook
    {
    readonly Microsoft.Office.Interop.Outlook.Items       _items;
    readonly Microsoft.Office.Interop.Outlook.NameSpace   _ns;
    readonly Microsoft.Office.Interop.Outlook.MAPIFolder  _inbox;
    readonly Microsoft.Office.Interop.Outlook.Application _application = new Microsoft.Office.Interop.Outlook.Application(); 

    public Outlook()
    {
        _ns    = _application.Session;
        _inbox = _ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
        _items = _inbox.Items;

        foreach (var item in _items)
        {
            string subject= string.Empty;
            var mail    = item as Microsoft.Office.Interop.Outlook.MailItem;
            if (mail    != null)
                var subject = mail.Subject;
            else
                Debug.WriteLine("Item is not a MailItem");
        }
    }
    }

请注意,在 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?

    public class Outlook
    {
    readonly Microsoft.Office.Interop.Outlook.Items       _items;
    readonly Microsoft.Office.Interop.Outlook.NameSpace   _ns;
    readonly Microsoft.Office.Interop.Outlook.MAPIFolder  _inbox;
    readonly Microsoft.Office.Interop.Outlook.Application _application = new Microsoft.Office.Interop.Outlook.Application(); 

    public Outlook()
    {
        _ns    = _application.Session;
        _inbox = _ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
        _items = _inbox.Items;

        foreach (var item in _items)
        {
            string subject= string.Empty;
            var mail    = item as Microsoft.Office.Interop.Outlook.MailItem;
            if (mail    != null)
                var subject = mail.Subject;
            else
                Debug.WriteLine("Item is not a MailItem");
        }
    }
    }

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

空袭的梦i 2024-10-18 13:16:31

好的!稍微调整了解决方案,这对我来说效果很好

foreach (dynamic item in mailItems)
        {
            if (item is MailItem)
            {
                Response.Write("Sender: ");
                Response.Write(item.SenderEmailAddress);
                Response.Write(" - To:");
                Response.Write(item.To);
                Response.Write("<br>");
            }
        }

Nice! adapted the solution a bit, this worked well for me

foreach (dynamic item in mailItems)
        {
            if (item is MailItem)
            {
                Response.Write("Sender: ");
                Response.Write(item.SenderEmailAddress);
                Response.Write(" - To:");
                Response.Write(item.To);
                Response.Write("<br>");
            }
        }
强者自强 2024-10-18 13:16:31

我遇到了这个问题,因为我的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文