Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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);
 
            //login
            mapiNameSpace.Logon(null, null, false, false);
 
            mapiNameSpace.Logon("yourMailAddress", "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)
            {
                Microsoft.Office.Interop.Outlook.Items items = myInbox.Items;
                inboxItems = inboxItems.Restrict("[UnRead] = true");
                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));
                    System.Threading.Thread.Sleep(1000);
                }
                Console.ReadKey();
Posted
Updated 18-Aug-14 4:50am
v2

1 solution

C#
Hi, I'm using this method, but I need it's runs all the time, I use a "while()" instead of "if(myInbox.Items.Count > 0)", so, when the cycle is used 21 times throws me an error.

Was not controlled System.Runtime.InteropServices.COMException
  Message=The operation failed due to a registry problem or installation. Restart Outlook and try again. If the problem persists, reinstall it.
  Source=Microsoft Outlook
  ErrorCode=-2147221225
  StackTrace:
       in Microsoft.Office.Interop.Outlook.ItemsClass.Restrict(String Filter)
       in LeerCorreos.Program.Main(String[] args) en C:\Users\Usuario\documents\visual studio 2010\Projects\LeerCorreos\LeerCorreos\Program.cs:línea 46
       in System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       in System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       in System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       in System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       in System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Where throws the error is in this line:

inboxItems = inboxItems.Restrict("[UnRead] = true"); 

can you help?, I really appreciate it, I don't know how to resolve it
 
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