Warm tip: This article is reproduced from stackoverflow.com, please click
email office365 outlook powershell

Create New Outlook 365 Email in PowerShell

发布于 2020-04-15 10:33:12

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?

Questioner
Randy
Viewed
43
Dmitry Streblechenko 2020-02-04 06:24

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.