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

try
        {
            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.....

Recommended Answers

All 3 Replies

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

for (int i = 0; i<myInbox.Items.Count; i++)
{
  ....
}

thanx Dear :-) I got it

Rishi_Hamza,

Please avoid duplication of post. If you have any question then ask us without any hesitation. If you want to know more about the forum and its rules then read - http://www.daniweb.com/forums/faq.php?faq=daniweb_policies.

If you are satisfy with the answer then hit "Mark as Solved". A link "Mark as Solved" is located at the end of thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.