title: Console App to search outlook inbox with subject line filter skipping emails
body: <p>I am writing a console application that searches through the current users outlook inbox.  The program performs certain actions on each email based on the subject line and then moves the email to 1 of 3 subfolders.  So far it has been running without throwing any errors.  The problem is that it will skip over some of the emails in the inbox and I can't seem to find any logic behind why it does this.  Sometimes it will skip the first email received, sometimes the last, other times it will leave 2 and move the rest.  I've inserted a breakpoint in my code and stepped through each line and the skipped emails never show up.  It doesn't seem to matter whether the emails are marked read or unread either.  If I run the program multiple times then eventually the skipped emails are processed.  What could be causing this?  </p>
  
  <pre><code>        Microsoft.Office.Interop.Outlook.Application application = null;          
  
          if (Process.GetProcessesByName("OUTLOOK").Count() &gt; 0)
          {  
              application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
          }
          else  
          {
              //outlook not open, do nothing
          }         
  
          Microsoft.Office.Interop.Outlook.Items items = null;
          Microsoft.Office.Interop.Outlook.NameSpace ns = null;
          Microsoft.Office.Interop.Outlook.MAPIFolder inbox = null;
          Microsoft.Office.Interop.Outlook.Application();
  
          ns = application.Session;
          inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
          items = inbox.Items;
  
          try
          {                
              foreach (Object receivedMail in items)
              {                   
                  if (receivedMail is MailItem)
                  {                        
                      MailItem mail = receivedMail as MailItem;
  
                          string subject = mail.Subject.ToString().ToUpper();
  
                          switch (subject)
                          {
                              case "SUBJECT1":
                                  DoStuff1(mail);           
                                  mail.Move(inbox.Folders["folder1"]);   
                                  break;
                              case "SUBJECT2":
                                  DoStuff2(mail);                                    
                                  mail.Move(inbox.Folders["folder2"]);
                                  break;
                              default:
                                  mail.Move(inbox.Folders["folder3"]);          
                                  break;
  
                      }
                  }
              }
  
              Console.WriteLine("Complete");
              Console.ReadLine();
          }
          catch (System.Runtime.InteropServices.COMException ex)
          {
              Console.WriteLine(ex.ToString());
              Console.ReadLine();
          }
          catch (System.Exception ex)
          {
              Console.WriteLine(ex.ToString());
              Console.ReadLine();
          }   
  </code></pre>