title: C# COM Exception when creating Outlook 2007 Application instance (80010001, ErrorCode -2147418111)
body: <p>I've created a Winforms application to schedule the execution of some Crystal Reports. This application is scheduled to execute under my Windows Domain user using the standard Windows XP Scheduled Tasks function.</p>
  
  <p>I've added the ability for my application to send emails in the event that a failure occurs during the execution of a report, and also a completion email once all of the scheduled reports have executed for that day.</p>
  
  <p>The following code shows a test of how I'm going about creating an instance of Microsoft.Office.Interop.Outlook.Application, to then create an _MailItem for population and sending:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using Outlook = Microsoft.Office.Interop.Outlook;
  
  namespace MailTest
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              Boolean eResult = false;
              String emailError = "";
  
              eResult = sendEmail("blah@blah.com", "Email Testing", "This is a test", false, out emailError);
  
              if (eResult)
                  MessageBox.Show("Email sent!");
              else
                  MessageBox.Show("Email failed-&gt;" + emailError);
          }
  
          public static Boolean sendEmail(String recipients, String subject, String body, Boolean highImportance, out String error)
          {
              Boolean success = true;
              error = "";
  
              Outlook.Application outApp = null;
  
              // Create an instance of the Outlook Application.
              try
              {
                  outApp = new Outlook.Application();
              }
              catch (System.Runtime.InteropServices.COMException ce)
              {
                  // We're going to do nothing with this at this time, as something weird happens if Outlook is not
                  // open when outApp is created above, and a COM exception is raised - however, Outlook ends up
                  // being opened, and creating an Application instance again succeeds. This is a workaround until I can
                  // find more detail on the COM exception.
                  MessageBox.Show(ce.Message);
              }
              catch (System.Exception e)
              {
                  // Any other type of Exception should be reported back to the caller with a failure status.
                  error = e.Message;
                  return false;
              }
  
              // Re-try the setting of outApp in case the original attempt failed above.
              if (outApp == null)
              {
                  try
                  {
                      outApp = new Outlook.Application();
                  }
                  catch (Exception e)
                  {
                      // If we fail to successfully open Outlook the second time, a different error has occurred, so
                      // we just want to return the exception message with false as result.
                      error = e.Message;
                      return false;
                  }
              }
  
              // Now create an Outlook mail item to populate and send.
              Outlook.MailItem msg = outApp.CreateItem(Outlook.OlItemType.olMailItem);
              // etc etc
  </code></pre>
  
  <p>When the first attempt to create a new Outlook Application is reached, and Outlook is not running on my PC already, Outlook is opened (I see it's icon appear in the System Tray), but the following exception is raised within the line </p>
  
  <pre><code>outApp = new Outlook.Application();
  </code></pre>
  
  <p>Exception Details:</p>
  
  <pre><code>System.Runtime.InteropServices.COMException was unhandled
    Message=Creating an instance of the COM component with CLSID {0006F03A-0000-0000-C000-000000000046} from the IClassFactory failed due to the following error: 80010001.
    Source=mscorlib
    ErrorCode=-2147418111
    StackTrace:
         at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
         at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
         at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
         at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean&amp; canBeCached, RuntimeMethodHandleInternal&amp; ctor, Boolean&amp; bNeedSecurityCheck)
         at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
         at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
         at System.Activator.CreateInstance(Type type, Boolean nonPublic)
         at System.Activator.CreateInstance(Type type)
         at MailTest.Form1.sendEmail(String recipients, String subject, String body, Boolean highImportance, String&amp; error) in c:\Documents and Settings\CT100751\My Documents\Visual Studio 2010\Projects\MailTest\MailTest\Form1.cs:line 43
         at MailTest.Form1.button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\CT100751\My Documents\Visual Studio 2010\Projects\MailTest\MailTest\Form1.cs:line 25
         at System.Windows.Forms.Control.OnClick(EventArgs e)
         at System.Windows.Forms.Button.OnClick(EventArgs e)
         at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
         at System.Windows.Forms.Control.WmMouseUp(Message&amp; m, MouseButtons button, Int32 clicks)
         at System.Windows.Forms.Control.WndProc(Message&amp; m)
         at System.Windows.Forms.ButtonBase.WndProc(Message&amp; m)
         at System.Windows.Forms.Button.WndProc(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
         at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
         at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
         at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)
         at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
         at System.Windows.Forms.Application.Run(Form mainForm)
         at MailTest.Program.Main() in c:\Documents and Settings\CT100751\My Documents\Visual Studio 2010\Projects\MailTest\MailTest\Program.cs:line 18
         at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
         at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         at System.Threading.ThreadHelper.ThreadStart()
    InnerException: 
  </code></pre>
  
  <p>I've searched online for the combination of error 80010001 and ErrorCode -2147418111, and have found nothing.</p>
  
  <p>However, as Outlook is now open, if I attempt to create the Application instance again, I succeed, and the email can then be sent successfully.</p>
  
  <p>I'm not happy with this work-around, and would prefer to understand the cause of the COM Exception to see if I can resolve it.</p>
  
  <p>Note that this is internal corporate email, so I don't have access to an SMTP server and therefore I have to use Outlook for sending.</p>
  
  <p>Anyone know what's going on here?</p>