Search code examples
powershellemailoutlookoffice-automationmicrosoft-information-protection

Adding Unified Label to outgoing Outlook email via Powershell script


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.


Solution

  • You have mixed the property name and its value. What you are trying to add as a property should be used as a property value. The last error message clearly indicate that:

    A field name cannot contain the following characters: [, ], _, and #.
    

    For example, the following code can be used instead:

    propertyAccessor.SetProperty("http://schemas.microsoft.com/mapi/string/{00020386-0000-0000-C000-000000000046}/msip_labels/0x0000001F", yourPropertyText);
    

    You can use the UserProperty.Value property to set a property value after adding it.

    You also can use the SetProperty method instead as shown in my sample code above, it sets the property specified by SchemaName to the value specified by Value. To set for the first time a property created by the UserProperties.Add method, use the UserProperty.Value property instead of the SetProperties or SetProperty method of the PropertyAccessor object.

    propertyAccessor.SetProperty myProp, myValue
    

    If the property does not exist and the SchemaName contains a valid property specifier, then SetProperty creates the property and assigns the value specified by Value. If the property does exist and SchemaName is valid, then SetProperty assigns the property with the value specified by Value.

    The name of the property is a string with the maximum length of 64 characters. The characters, '[', ']', '_' and '#', are not permitted in the name.