Randy
Randy

Reputation: 1136

Create New Outlook 365 Email in PowerShell

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?

Upvotes: 0

Views: 1498

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66225

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.

Upvotes: 2

Related Questions