title: How do i access an outlook account inbox using c#?
body: <p>How do i access an outlook account inbox using c#? I've tried using the outlook object model and imap but it only displays the outlook account logged into my machine. I try logging into a different account with  its username and password but it still logs into my local machine account. I read that logging out and closing outlook should fix this problem but it didn't change the results</p>
  
  <p>Here is the code</p>
  
  <pre><code>   using System;
     using System.Reflection;
  
     using Outlook = Microsoft.Office.Interop.Outlook;
  
     namespace ConsoleApplication1
     {
         public class Class1
         {
             public static int Main(string[] args)
             {
                 try
                 {
  
                     Outlook.Application oApp = new Outlook.Application();
  
                     // Get the MAPI namespace.
                     Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");             
                     oNS.Logon("email address placeholder", "password placeholder", false, true);
  
                     //Get the Inbox folder.
                     Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                     String user = oNS.CurrentUser.EntryID;
  
                     //Get the Items collection in the Inbox folder.
                     Outlook.Items oItems = oInbox.Items;
  
                     Console.WriteLine(oItems.Count);               
                     Outlook.MailItem oMsg = (Outlook.MailItem)oItems.GetFirst();
  
                     Console.WriteLine(oMsg.Subject);
                     Console.WriteLine(oMsg.SenderName);
                     Console.WriteLine(oMsg.ReceivedTime);
                     Console.WriteLine(oMsg.Body);
  
                     int AttachCnt = oMsg.Attachments.Count;
                     Console.WriteLine("Attachments: " + AttachCnt.ToString());
  
                     if (AttachCnt &gt; 0) 
                     {
                     for (int i = 1; i &lt;= AttachCnt; i++) 
                      Console.WriteLine(i.ToString() + "-" + oMsg.Attachments[i].DisplayName);
                     }
  
              for (int i = 0; i &lt; oItems.Count; i++)
              {
                  if (oItems.GetNext() is Outlook.MailItem)
                  {
                      oMsg = (Outlook.MailItem)oItems.GetNext();
                      Console.WriteLine(oMsg.Subject);
                      Console.WriteLine(oMsg.SenderName);
                      Console.WriteLine(oMsg.ReceivedTime);
                      Console.WriteLine(oMsg.Body);
                      AttachCnt = oMsg.Attachments.Count;
                      if (AttachCnt &gt; 0)
                      {
                          for (int j = 1; j &lt;= AttachCnt; j++)
                              Console.WriteLine(j.ToString() + "-" + oMsg.Attachments[j].DisplayName);
                      }
  
                      Console.WriteLine("Attachments: " + AttachCnt.ToString());
                      Console.WriteLine("CURRENT EMAIL # IS: " + i);
                  }
                 else
                  {
                      oItems.GetNext();
                      Console.WriteLine("NOT AN EMAIL");
                      Console.WriteLine("CURRENT EMAIL # IS: " + i);
                  }
  
  
              }
  
              oNS.Logoff();
  
              oMsg = null;
              oItems = null;
              oInbox = null;
              oNS = null;
              oApp = null;
          }
  
          catch (Exception e)
          {
              Console.WriteLine("{0} Exception caught: ", e);
  
  
          }
  
          return 0;
  
      }
  }
  </code></pre>
  
  <p>}</p>