如何在C#中从outlook中检索邮件时删除COM异常?

时间:2013-08-24 09:39:39

标签: c# listview outlook-2007

当我在控制台应用程序中执行下面的代码时,它会给出准确的结果,但是当我在Windows窗体中尝试它时,它会在Outlook上引发COM异常.Application app = new Outlook.Application();

异常是“检索具有CLSID {0006F03A-0000-0000-C000-000000000046}的组件的COM类工厂因以下错误而失败:80080005服务器执行失败(HRESULT异常:0x80080005(CO_E_SERVER_EXEC_FAILURE))。”

 if (comboBoxFolder.SelectedIndex == 0)
        {
            setInboxView();

                Outlook.Application app = new Outlook.Application();
                Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
                Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

                foreach (Outlook.MailItem item in emailFolder.Items)
                {
                    mails[0] = item.SenderEmailAddress;
                    mails[1] = item.To;
                    mails[2] = item.Subject;
                    mails[3] = Convert.ToString(item.ReceivedTime);

                }

1 个答案:

答案 0 :(得分:0)

我通过在Private之外创建Outlook.Application实例找到了解决方案。 它删除了该异常

string[] mails = new string[4];
    ListViewItem itm;
    Private Outlook.Application app = new Outlook.Application();

    private void comboBoxFolder_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (comboBoxFolder.SelectedIndex == 0)
        {

                Outlook.NameSpace outlookNs = app.GetNamespace("MAPI");
                Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

                foreach (Outlook.MailItem item in emailFolder.Items)
                {
                    mails[0] = item.SenderEmailAddress;
                    mails[1] = item.To;
                    mails[2] = item.Subject;
                    mails[3] = Convert.ToString(item.ReceivedTime);

                    itm = new ListViewItem(mails);
                    listViewEmail.Items.Add(itm);

                }



        }
    }