I am trying to import mails from Outlook to my Database during execution of code it is throwing the "Excepiton ComException unhandeled by User Code" Array index out of bounds.
it was working fine in the biggening but suddenly start throwing this exception.

Here is my code

            Microsoft.Office.Interop.Outlook.Application MYAPP = new Microsoft.Office.Interop.Outlook.ApplicationClass();
            Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = MYAPP.GetNamespace("MAPI");
            Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
for (int i = 0; i <= myInbox.Items.Count; i++)
                lblSubject = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i]).Subject; //throws exception here
                txtBody = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i]).Body;
               // if (((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i]).CC.Length > 0)
                Mail_CC = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i]).CC;

 catch (System.Exception ex)


string error = ex.Message;


I was Having the same problem few days back when Dear MikiUrban Help me out. but I have still the problem when I start reading mail from OutLook from SUBJECT Line its throws the base {System.Runtime.InteropServices.ExternalException} = "Array index out of bounds", if I comment the Subject Line then same error in 'Body' while reading the next item of mail. Please anybuddy Help me I will be very thankful.....

Edited 7 Years Ago by Rishi_Hamza: n/a

Change an expression: i<=myInbox.Items.Count; with,

for (int i = 0; i<myInbox.Items.Count; i++)
This article has been dead for over six months. Start a new discussion instead.