Click here to Skip to main content
15,671,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For read and parse (subject, body, attachments ...) an email from Outlook (add in C#), I tried to write this function :

using Outlook = Microsoft.Office.Interop.Outlook; 
public void readEmail()
    Outlook.Application myApp = new Outlook.ApplicationClass();
    Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
    Outlook.MAPIFolder myInbox =         mapiNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
    MessageBox.Show(myInbox.Items.Count + "");
    //Read all the email one by one
    //for (int i = myInbox.Items.Count; i >= (myInbox.Items.Count-1); i--)
      //String strSubject = ((Outlook.MailItem)myInbox.Items[i]).Subject;
      // Sender Email
      //string senderEmailid = ((Outlook.MailItem)myInbox.Items[i]).SenderEmailAddress;
      //string CreationTime=(( //Outlook.MailItem)myInbox.Items[i]).CreationTime.ToString();

      //string strEmailBody=(( Outlook.MailItem)myInbox.Items[i]).Body;

      //string strEmailSenderName=(( Outlook.MailItem)myInbox.Items[i]).SenderName;
  catch(Exception ex)

but "MyInbox.Items.Count" is 0 and if I try to display one of the variables (ex : strEmailBody), it gives an exception : Array index out of bound.

Can someone help me please ???
Updated 24-Feb-20 19:16pm
jo.him1988 9-Jul-14 9:46am    
how are you access your outlook account did you forgot to mention it
mapiNameSpace.Logon(null, null, false, false);

mapiNameSpace.Logon("your outlook mail id", "password", Missing.Value, true);

here is a code script:-

1 solution

 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 myContacts = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);

            mapiNameSpace.Logon(null, null, false, false);

            mapiNameSpace.Logon("YourOutlookMailID", "Password", Missing.Value, true);

            Microsoft.Office.Interop.Outlook.Items myItems = myContacts.Items;

           // Console.WriteLine("Total : ", myItems.Count);

            Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

          //  Console.Write(myInbox.Name);

            Microsoft.Office.Interop.Outlook.Items inboxItems = myInbox.Items;

           // Console.WriteLine("Total : ", inboxItems.Count);

            Microsoft.Office.Interop.Outlook.Explorer myexp = myInbox.GetExplorer(false);

            mapiNameSpace.Logon("Profile", Missing.Value, false, true);

            if (myInbox.Items.Count > 0)
            Console.WriteLine(string.Format("Total Unread message {0}:", inboxItems.Count));
                int x=0;
                foreach (Microsoft.Office.Interop.Outlook.MailItem item in inboxItems)
                    Console.WriteLine("{0} unread mail from your inbox", ++x);
                    Console.WriteLine(string.Format("from:-       {0}", item.SenderName));
                    Console.WriteLine(string.Format("To:-         {0}", item.ReceivedByName));
                    Console.WriteLine(string.Format("Subject:-     {0}", item.Subject));
                    Console.WriteLine(string.Format("Message:-     {0}", item.Body));


happy coding :) :) :)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900