title: Send Outlook Mail (2013) Not working
body: <p>I am writing a script that at the end will send an email with logs from a completed process.  We use Outlook 2013 at work and I'm testing this on both outlook 2013 and Outlook 2010 with the same results.</p>
  
  <p>The script will only generate a mailitem if Outlook is closed.  If it is open it throws a bunch of errors.  Anyone have an idea?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>############ Setup the outlook object#####
  
  $outlook = New-Object -ComObject Outlook.Application
  $datestr = get-date -Format yyyyMM
  $message = $outlook.CreateItem(0)
  
  ###### Create an email and add the logs as attachments ######
  
  $toaddress1 = "nospam@example.com"
  $msub = "Monthly load - logs attached $datestr" 
  $message.Recipients.Add($toaddress1)  
  $message.Subject = "$msub"  
  $message.htmlbody = "See attached logs for this months file load&lt;br&gt;&lt;br&gt;" 
  
  $filepath = get-childitem  "c:\path\log" | ? {$_.PSISContainer -eq $false} | select -ExpandProperty fullname
      foreach($file in $filepath) {
  
  $message.Attachments.Add($file)
  $message.display()
    } 
  </code></pre>
  
  <p>Like I said, it works when Outlook is not open but with it open it generates the following errors from PowerShell:</p>
  
  <pre class="lang-none prettyprint-override"><code>New-Object : Retrieving the COM class factory for component with CLSID
  {0006F03A-0000-0000-C000-000000000046} failed due to the following error:
  80080005 Server execution failed (Exception from HRESULT: 0x80080005
  (CO_E_SERVER_EXEC_FAILURE)).
  At c:\scripts\mailtest.ps1:4 char:13
  + $message = (New-Object -ComObject Outlook.Application)
  +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : ResourceUnavailable: (:) [New-Object], COMException
      + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
  
  Exception calling "CreateItem" with "1" argument(s): "The RPC server is
  unavailable. (Exception from HRESULT: 0x800706BA)"
  At c:\scripts\mailtest.ps1:7 char:1
  + $message = $outlook.CreateItem(1)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : COMException
  
  You cannot call a method on a null-valued expression.
  At c:\scripts\mailtest.ps1:13 char:1
  + $message.Recipients.Add($toaddress1)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
  At c:\scripts\mailtest.ps1:14 char:1
  + $message.Subject = "$msub"
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OperationStopped: (:) [], COMException
      + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
  
  The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
  At c:\scripts\mailtest.ps1:15 char:1
  + $message.htmlbody = "Jeff attached are the logs from this months load ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OperationStopped: (:) [], COMException
      + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
  
  You cannot call a method on a null-valued expression.
  At c:\scripts\mailtest.ps1:20 char:1
  + $message.Attachments.Add($file)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
  At c:\scripts\mailtest.ps1:21 char:1
  + $message.display()
  + ~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OperationStopped: (:) [], COMException
      + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
  
  You cannot call a method on a null-valued expression.
  At c:\scripts\mailtest.ps1:20 char:1
  + $message.Attachments.Add($file)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
  At c:\scripts\mailtest.ps1:21 char:1
  + $message.display()
  + ~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OperationStopped: (:) [], COMException
      + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
  
  You cannot call a method on a null-valued expression.
  At c:\scripts\mailtest.ps1:20 char:1
  + $message.Attachments.Add($file)
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : InvokeMethodOnNull
  
  The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
  At c:\scripts\mailtest.ps1:21 char:1
  + $message.display()
  + ~~~~~~~~~~~~~~~~~~
      + CategoryInfo          : OperationStopped: (:) [], COMException
      + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
  </code></pre>
  
  <p>I went on MSDN and could not find any updated methods for <code>Outlook.Application</code> so it looks like I'm doing this right or maybe a better way?  In this case Outlook is always going to be open in our environment.</p>