Question

When I execute below code in console application it gives the accurate result but when I try it in windows form it throws COM exception on Outlook.Application app = new Outlook.Application();

The Exception is "Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from 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);

                }
Was it helpful?

Solution

I have find the solution by creating instance of Outlook.Application outside Private.. It removes that exception

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

                }



        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top