title: Extracting/Scanning emails from Outlook using C#
body: <p>I'm trying to make a windows form application that can be "hidden" in the taskbar (like WiFi and so on), equipped with a timer, and that every 10 seconds it scans my email inbox in Outlook.</p>
  
  <p>The first part works fine, but I can't get the scan to work. At first i just want to extract the names of email subjects and put them into a text file, just to test the code. But in the end i'd like to scan one particular inbox (i have several on my outlook, like 5 or 6, with different mail address associeted, and i can't find anything on the internet to target one of them) and make a popup or something when particular email are received.</p>
  
  <p>Anyway, this is the code i have so far:</p>
  
  <pre><code>    public static bool isRunning = false;
  
      public Form1()
      {
          InitializeComponent();
  
          System.Timers.Timer timer = new System.Timers.Timer(10000);
          timer.Elapsed += OnTimedEvent;
          timer.Enabled = true;
      }
  
  
      private void Hide_Click(object sender, EventArgs e)
      {
          this.Hide();
          notifyIcon1.Visible = true;
      }
  
      private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
      {
          this.Show();
          notifyIcon1.Visible = false;
      }
  
      private static void OnTimedEvent(object source, ElapsedEventArgs e)
      {
          Outlook.Application app = null;
          Outlook.MAPIFolder inbox = null;
          Outlook._NameSpace ns = null;
  
          if (Process.GetProcessesByName("OUTLOOK").Count() &gt; 0)
          {
              try
              {
                  app = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
                  inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                  ns = app.GetNamespace("MAPI");
                  List&lt;Outlook.MailItem&gt; ReceivedEmail = new List&lt;Outlook.MailItem&gt;();
                  List&lt;string&gt; titles = new List&lt;string&gt;();
  
                  foreach (Outlook.MailItem mail in inbox.Items)
                  {
                      ReceivedEmail.Add(mail);
                  }
  
                  foreach (Outlook.MailItem mail in ReceivedEmail)
                  {
                      titles.Add(mail.Subject.ToString());
                  }
  
                  File.WriteAllLines("C://Users/A222946/Desktop/allMails.txt", titles);
  
              }
              catch (System.Runtime.InteropServices.COMException ex)
              {
                  MessageBox.Show(ex.Message);
              }
              catch (System.Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
          else
          {
              MessageBox.Show("Please, start outlook..");
          }
  
      }
  }
  </code></pre>
  
  <p>The error i find when i'm running this is the following:</p>
  
  <blockquote>
    <p>Operation unavailable (Exception from HRESULT: 0x800401E3 (MK_E_UNAVAILABLE)</p>
  </blockquote>
  
  <p>I tried with and without administrator rights, same error.</p>
  
  <p><strong>Update</strong></p>
  
  <p>So after some changes it looks like this now:</p>
  
  <pre><code>private static void OnTimedEvent(object source, ElapsedEventArgs e)
      {
          Outlook.Application app = new Outlook.Application();
          Outlook.MAPIFolder inbox = null;
          Outlook._NameSpace ns = null;
          Outlook.Items items = null;
  
          if (Process.GetProcessesByName("OUTLOOK").Count() &gt; 0)
          {
              try
              {
                  app = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
                  ns = app.GetNamespace("MAPI");
                  inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                  items = inbox.Items;
                  List&lt;Outlook.MailItem&gt; ReceivedEmail = new List&lt;Outlook.MailItem&gt;();
                  List&lt;string&gt; titles = new List&lt;string&gt;();
  
                  foreach (Object obj in items)
                  {
                      if (obj is Outlook.MailItem)
                      {
                          ReceivedEmail.Add((Outlook.MailItem)obj);
                      } 
                  }
  
                  foreach (Outlook.MailItem mail in ReceivedEmail)
                  {
                      titles.Add(mail.Subject.ToString());
                  }
  
                  File.WriteAllLines("C://Users/A222946/Desktop/allMails.txt", titles);
  
              }
              catch (COMException ex)
              {
                  MessageBox.Show(ex.Message);
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
          else
          {
              MessageBox.Show("Please, start outlook..");
          }
  
      }
  </code></pre>
  
  <p>But i still have this error:</p>
  
  <blockquote>
    <p>object reference not set to an instance of an object</p>
  </blockquote>
  
  <p>Also, do you have any idea how i could target one particular mailbox? Example: "abcdefg@blabla.com"</p>