0
votes

I'm trying to create an Outlook email in PowerShell, and I've found the same code everywhere to do it:

$ol = New-Object -comObject Outlook.Application

$mail = $ol.CreateItem(0)
$mail.Subject = "<Subject>"
$mail.Body = "<Body>"

and then either

$inspector = $mail.GetInspector
$inspector.Display()

Or

$mail.Display()

to show the email.

However, at the very first line I get this error:

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)).

I've tried it without the -comObject and get a different error:

New-Object : Cannot find type [Outlook.Application]: verify that the assembly containing this type is loaded.

I've tried loading the assembly with

[Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Outlook") | out-null

but still get the same error messages when running the previous commands.

In case it matters, we are using Office 365, but I do have a local copy of Office installed. Is there a different type of object I need to use with Office 365?

Also, it looks like it's trying to reach out to a server to create the object. Is there a way for me to force it to do so locally?

1
Why do you want to use Outlook ? Can't you send your message through Send-MailMessage directly (and if you weren't aware of the latter, then I suggest using that to send your messages) ?Sage Pourpre
Does your company have an smtp server?Evan
Does it actually need to be interactive - i.e. do you need to see the email pop up in front of you? or are you just trying to send an email? any interactivity will require a client installed and can't be automated reliably.Nick.McDermaid
To answer all 3 questions... Yes, I want it to be interactive - mostly so that the final email can be looked over, and possibly edited, prior to sending - which is why I'm not using Send-MailMessage. Again, I do have a client installed already. And we do not have a local SMTP server, however one is provided with Office 365.Randy

1 Answers

1
votes

CO_E_SERVER_EXEC_FAILURE means Outlook is running in a security context different from that of your app. COM system refuses to marshal calls between processes running in different security contexts.