我已经编写了此代码以在我的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循环中,我遇到了这个错误:

“无法将com对象施加type'system .__ comobject'到接口type'microsoft.office.interop.outlook.mailitem'。此操作失败,因为与IID接口上的com Component在com Component上调用IID'{00063034-0000-0000-0000-0000--0000-0000- 0000-C000-000000000046}'由于以下错误而失败:没有支持此类接口(Hresult的例外:0x80004002(e_nointerface))。

您能帮助我如何解决此错误吗?

有帮助吗?

解决方案

不久前,我必须解决您的问题。

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

希望有帮助。

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

其他提示

尝试检查项目是有效的 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());
    }
}

测试时,以下代码正常工作。但是我必须提到我的引用是“ 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中,许多项目具有一些共同的属性(例如到期时间),因此您可以作为绝望的解决方法,使用“动态”数据类型 - 作为未知项目类型的后备方案,或者是您的默认情况(很长)当您对性能的热门效果很好)。

好的!调整了一些解决方案,这对我来说很好

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>");
            }
        }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top