title: Parse Outlook email in Inbox which belongs to other account
body: <p>I am trying to create a service which will run as a background service on a server. This service will parse email as soon as it enters the Inbox.</p>
  
  <p>We have one email account abc@organization.com. We are using Outlook to check the emails. It is a service so Outlook won't be running all the time on the server. I want to parse the email automatically for this account. This account is not my email account. I am using Microsoft.Office.Interop.outlook in C# program.</p>
  
  <p>This program is running but it is parsing email from my email inbox. I don't know how to specify specific email to parse the Inbox. Need to know the event which triggers as soon as new mail arrives. My program parsed half the emails from my inbox but after that it is throwing object null reference error.</p>
  
  <pre><code>static void Main(string[] args)
      {
          Microsoft.Office.Interop.Outlook.Application myApp=new Microsoft.Office.Interop.Outlook.Application();
          try
          {
              String Subject, Body, Createdate, Sender = null; 
              Microsoft.Office.Interop.Outlook.NameSpace mapinamespace = myApp.GetNamespace("MAPI");
  
              mapinamespace.Logon(null, null,true,true);
              Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapinamespace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
              Microsoft.Office.Interop.Outlook.Accounts accounts = myApp.Session.Accounts;
              try
              {
                  foreach (Microsoft.Office.Interop.Outlook.Account account in accounts)
                  {
                      if (account.SmtpAddress == "abc@organization.com")
                      {
                          Console.Write(account);
                      }
                      else
                      {
                          Console.Write("Not found ---");
                      }
                  }
              }
              catch{
              throw new System.Exception(string.Format("No account with amtp:{0} exists!"));}
  
              foreach (object item in myInbox.Items)
              {
                  try
                  {
                      var mail = item as MailItem;
                      if (mail != null)
                      {
                          //Creation date
                          Createdate = mail.CreationTime.ToString();
                          Console.WriteLine("CreationTime---" + Createdate);
                          //Grab the senders address
                          Sender = mail.SenderEmailAddress.ToString();
                          Console.WriteLine("Sender's E-mail---" + Sender);
                          //grab the Subject
                          Subject = mail.Subject.ToString();
                          Console.WriteLine("Subject--" + Subject);
                          //Grab the body
                          Body = mail.Body.ToString();
                          Console.WriteLine("Body---" + Body);
  
                          //Grab the Attachment
  
                      }
                      else
                      {
                          Console.Write("Error in mail---");
                      }
                  }
                  catch (System.Runtime.InteropServices.COMException e)
                  {
                      Console.Write(e);
                  }
              }
          }
          catch (System.Runtime.InteropServices.COMException e)
          {
              Console.Write(e);
          }
  
  
  
      }
  </code></pre>