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".
it was working fine in the beggening but suddenly start throwing this exception.

Here is my code

for (int i = 0; i <= myInbox.Items.Count; i++)
            {
                lblSubject = ((Microsoft.Office.Interop.Outlook.MailItem)myInbox.Items[i]).Subject.ToString(); //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;
}

please help me.

Recommended Answers

All 3 Replies

Unless someone here has specific knowledge of common errors reading Outlook, you should wrap all of that with a try/catch block, and examine the exception that is caught. You should get a better idea of that the problem is.

try{
... all your code ...
} catch (Exception ex)
{ 
string error = ex.Message;
}

It could be anything from an invalid cast, to no subject in the email, to trying assign a text string to a Label object (as a side note, your line should probably be changed to lblSubject.Text = ... etc )

Thank You so much Dear mikiurban Your advices really works and my problem is solved... you got the rite thing there was no subject there in that mail. and I wrote the code in Try{} catch(){}
So nice of You :-)

Glad I could help!

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.