I have a function that goes into a larger Powershell script. This function constructs and sends an email using Outlook. I'm using Outlook instead of Send-MailMessage or straight to SMTP because I need to include the Microsoft Unified Label that my organization has implemented (Public, Private, etc).
From what I can piece together from various online sources it should be something like ths:
Function outlookSend {
$outlook = New-Object -ComObject Outlook.Application -Verbose:$false
$message = $outlook.CreateItem(0)
$message.GetInspector.Activate()
$message.To = $recipient
$message.Subject = $subject
$message.HtmlBody = $body
$message.UserProperties.Add("MSIP_Label_11111111-2222-3333-4444-555555555555_Enabled", olText, True)
$message.Send()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($outlook) | Out-Null
}
MSIP_Label_11111111-2222-3333-4444-555555555555_Enabled including the GUID of the label that I want to use. When I run this I get the error:
Exception setting "Add": Cannot convert the "olText" value of type "string" to type "Object".
At C:\scripts\slaMail.ps1:211 char:5
+ $emailMessage.UserProperties.Add("MSIP_Label_3a2fed65-62e7-46ea-a ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : RuntimeException
I then converted "olText" to an object using:
$olText = ('olText' | ConvertFrom-String)
and using this line:
$message.UserProperties.Add("MSIP_Label_11111111-2222-3333-4444-555555555555_Enabled", $olText)
But, that gave me the following error:
A field name cannot contain the following characters: [, ], _, and #.
At C:\scripts\slaMail.ps1:211 char:5
+ $emailMessage.UserProperties.Add("MSIP_Label_3a2fed65-62e7-46ea-a ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
I did try escaping the "_" and even making it into a variable like I did with olText, but neither worked. I'm not sure I'm on the right track here or going the wrong direction. Any help would be greatly appreciated.