Warm tip: This article is reproduced from serverfault.com, please click

Send email with Outlook application with cutomized permissions (Using PowerShell)

发布于 2020-11-20 10:33:10

I'm trying to create and send emails using the Outlook application with PowerShell applying Permissions for the email (Example: Encrypt-Only, Do Not Forward, etc.), is currently working on the outlook application (when creating the email manually), however, couldn't find a way to send emails using PowerShell applying this setting:

Permission templates in Outlook

I'm currently using the following code to create and send the emails (Working):

$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem("olMailItem")
$Mail.To = "test@test.com"
$Mail.Subject = "Test Email"
$Mail.Body = "Email sent using PowerShell"
$file = "C:\ExampleFolder\test.txt"
$Mail.Attachments.Add($file)
$Mail.Send()

Is there a way to apply this setting using PowerShell?

Questioner
A. Barbagallo
Viewed
0
A. Barbagallo 2020-12-01 17:09:17

I founded the solution for this, this solution involve two properties: "$Outlook.CreateItem("olMailItem").Permission" and "$Outlook.CreateItem("olMailItem").PermissionTemplateGuid", and work as follow:

  • If $Outlook.CreateItem("olMailItem").Permission is set to "0": The email is sent as unrestricted message, no PermissionTemplateGuid is needed for this value.
  • If $Outlook.CreateItem("olMailItem").Permission is set to "1": The email is sent as olDoNotForward automatically, no PermissionTemplateGuid is needed for this value.
  • If $Outlook.CreateItem("olMailItem").Permission is set to "2": The email require a PermissionTemplateGuid, therefore, it's required to get the GUID of the template, since each organization have a different template according with the configuration, send an email with the encryption template to your inbox and read it using the following code:

$olFolderInbox = 6
$outlook = new-object -com outlook.application
$mapi = $outlook.GetNameSpace("MAPI")
$inbox = $mapi.GetDefaultFolder($olFolderInbox)
$items = $inbox.items
$items[1].PermissionTemplateGuid

This is going to provide the correct permissionTemplateGuid with the desired encryption.

Therefore, to send the email the following formula works:

$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem("olMailItem")
$Mail.To = "test@test.com"
$Mail.Subject = "Test Email"
$Mail.Body = "Email sent using PowerShell"
$file = "C:\ExampleFolder\test.txt"
$Mail.Attachments.Add($file)
$Mail.PermissionTemplateGuid = XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
$Mail.Permission = "2"
$Mail.Send()

For more information visit this website: http://jon.glass/blog/reads-e-mail-with-powershell/