title: How to alter a MailItem being displayed in the preview pane or opened
body: <p>I am attempting to write an AddIn that can modify an Outlook MailItem when it is loaded (either through a preview pane or through it being opened). Here is the code that I have so far, but I can't seem to actually access the MailItem</p>
  
  <pre><code>public partial class ThisAddIn
  {
      private void ThisAddIn_Startup(object sender, EventArgs e)
      {
          Application.ItemLoad += ApplicationOnItemLoad;
      }
  
      private void ApplicationOnItemLoad(object item)
      {
          var mail = item as MailItem;
  
          if (mail != null)
          {
              Console.WriteLine(mail.HTMLBody);
          }
      }
  
      // Etc...
  }
  </code></pre>
  
  <hr>
  
  <p>The code reaches the <code>Console.WriteLine(mail.HTMLBody);</code> statement just fine, meaning that casting the item as an Outlook MailItem is fine (it doesn't end up being null). However, I can't access any of the MailItem members... it just throws exceptions:</p>
  
  <blockquote>
    <p>A first chance exception of type 'System.Runtime.InteropServices.COMException'
    occurred in MyOutlookProject.DLL
    <br />
    A first chance exception of type 'System.Reflection.TargetInvocationException'
    occurred in mscorlib.dll</p>
  </blockquote>