简体   繁体   中英

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

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?

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM