0

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

异常是“由于以下错误,检索具有 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);

                }
4

1 回答 1

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

                }



        }
    }
于 2013-08-24T10:24:09.657 回答