none
C# programm will throw exception when access outlook 2007 mailbox RRS feed

  • Question

  • System envrionment: windows 7.
    Outlook: Outlook 2007
     Visual stdio version: vs2013.

    In debug mode, C# program can access outlook mailbox normally.

    But if in without debug mode, C# program will throw exception

    ************** Exception Text **************
    System.Runtime.InteropServices.COMException (0x80040117): The operation failed because of a registry or installation problem. Restart Outlook and try again. If the problem persists, reinstall.
       at Microsoft.Office.Interop.Outlook._Items.Restrict(String Filter)
       at Scan_SmokeTestMail.Form1.scanSmokeTestMailToolStripMenuItem_Click(Object sender, EventArgs e) in d:\Program Files (x86)\Microsoft Visual Studio 12.0\Projects\Scan_SmokeTestMail\Scan_SmokeTestMail\Form1.cs:line 44
       at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
       at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
       at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
       at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
       at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
       at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ToolStrip.WndProc(Message& m)
       at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    Following is part C# program.

     private void scanSmokeTestMailToolStripMenuItem_Click(object sender, EventArgs e)
            {
                bool bScan = true;
                DateTime startScanTime = DateTime.Now;
                Outlook.Application myOutlookApp = new Outlook.Application();
                Outlook.NameSpace myNameSpace = myOutlookApp.GetNamespace("MAPI");
                Outlook.MAPIFolder myFolderInbox = myNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.Items oItems = myFolderInbox.Items;
                Outlook.MailItem newEmail = null;
                String sub;
                DateTime receivetime;
                string body;
                while (bScan)
                {
                    oItems = oItems.Restrict("[UnRead]=true");
                    int ncount = oItems.Count;
                    foreach (object collectionItem in oItems)
                    {
                        newEmail = collectionItem as Outlook.MailItem;
                        if (newEmail != null)
                        {
                            if (startScanTime <= newEmail.ReceivedTime)
                            {
                                MessageBox.Show("收到了新邮件, 标题为:" + newEmail.Subject);
                                bScan = false;
                            }
                        }
                    }
                }
            }

    • Moved by CoolDadTx Tuesday, October 10, 2017 4:22 PM Office related
    Monday, October 9, 2017 3:17 AM

Answers

  • Hello steven_wqs,

    Put the following statement out of while loop.

    oItems = oItems.Restrict("[UnRead]=true");
                int ncount = oItems.Count;

    The result should be like this.

    bool bScan = true;
                DateTime startScanTime = DateTime.Now;
                Microsoft.Office.Interop.Outlook.Application myOutlookApp = new Microsoft.Office.Interop.Outlook.Application();
                NameSpace myNameSpace = myOutlookApp.GetNamespace("MAPI");
                MAPIFolder myFolderInbox = myNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
                Items oItems = myFolderInbox.Items;
                MailItem newEmail = null;
    
                oItems = oItems.Restrict("[UnRead]=true");
                int ncount = oItems.Count;
    
                while (bScan)
                {
                    
                    foreach (object collectionItem in oItems)
                    {
                        newEmail = collectionItem as MailItem;
                        if (newEmail != null)
                        {
                            if (startScanTime <= newEmail.ReceivedTime)
                            {
                                MessageBox.Show("收到了新邮件, 标题为:" + newEmail.Subject);
                                bScan = false;
                            }
                        }
                    }
                }

    Sincerely,

    neil hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 9, 2017 10:28 AM
  • Hi steven_wqs,

    The above code you moved  is to filter you mail item, it is reasonable that you set the filter statement once.

    When you invoke the outlook exposed api to do meaningless setting  per milliseconds , OutLook will regard it as dangerous procedures. It will stop providing mail information for security.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 10, 2017 7:17 AM

All replies

  • If I run C#program with administration right, then no above exception, but have following exception:

    An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

    Additional information: 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)).

    Anybody know the reason?


    • Edited by steven_wqs Monday, October 9, 2017 7:59 AM
    Monday, October 9, 2017 7:58 AM
  • Hello steven_wqs,

    Put the following statement out of while loop.

    oItems = oItems.Restrict("[UnRead]=true");
                int ncount = oItems.Count;

    The result should be like this.

    bool bScan = true;
                DateTime startScanTime = DateTime.Now;
                Microsoft.Office.Interop.Outlook.Application myOutlookApp = new Microsoft.Office.Interop.Outlook.Application();
                NameSpace myNameSpace = myOutlookApp.GetNamespace("MAPI");
                MAPIFolder myFolderInbox = myNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
                Items oItems = myFolderInbox.Items;
                MailItem newEmail = null;
    
                oItems = oItems.Restrict("[UnRead]=true");
                int ncount = oItems.Count;
    
                while (bScan)
                {
                    
                    foreach (object collectionItem in oItems)
                    {
                        newEmail = collectionItem as MailItem;
                        if (newEmail != null)
                        {
                            if (startScanTime <= newEmail.ReceivedTime)
                            {
                                MessageBox.Show("收到了新邮件, 标题为:" + newEmail.Subject);
                                bScan = false;
                            }
                        }
                    }
                }

    Sincerely,

    neil hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 9, 2017 10:28 AM
  • Hello neil hu,

         Than you for your suggestion. I have tried your method and generate new C# program.

    Then run outlook 2007 and this C# program with normal right. No exception.

    Could you please tell me reason? thank you!

    Tuesday, October 10, 2017 5:48 AM
  • Hi steven_wqs,

    The above code you moved  is to filter you mail item, it is reasonable that you set the filter statement once.

    When you invoke the outlook exposed api to do meaningless setting  per milliseconds , OutLook will regard it as dangerous procedures. It will stop providing mail information for security.

    Sincerely,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 10, 2017 7:17 AM
  • Thank you for your answer.
    Sunday, December 17, 2017 11:45 PM