Sending an email with X-Headers in EWS via powershell

I recently needed to test sending an email via EWS with a bunch of custom X-headers. While I have worked with EWS in PowerShell before I couldn’t find any examples of how to do so but found plenty in Java and .Net. So, after stumbling through trying to recreate what I found in PowerShell I came with up with the following and inserted into this script for sending EWS emails.

In the code below, I’m

  1. Creating a new property set so I have access to edit all the parts of the new email. I think all the needed property sets must be loaded at the same time. That is you can’t load one set and add another without it overwriting the first property set
  2. I then save the email since I learned in my testing you can’t add a property to the email without an instance key associated with it, which it doesn’t get until it’s saved.
  3. Create a internet header object and it’s name
  4. Load the property set
  5. Insert the X-Header and its value in the headers of the email
  6. I then update the saved email (though I don’t think this necessary)

 

#Tested by using this : https://gallery.technet.microsoft.com/office/Send-e-mail-from-67a714aa

#TO view more properties

# https://msdn.microsoft.com/en-us/library/microsoft.exchange.webservices.data.emailmessage_members(v=exchg.80).aspx

$propertyset = New-Object Microsoft.Exchange.WebServices.Data.PropertySet (

    [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::InternetMessageHeaders,

    [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ExtendedProperties,

    [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::MimeContent,

    [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::Attachments,

    [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ToRecipients,

    [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::Body,

    [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::BccRecipients,

    [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::CcRecipients,

    [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::From,

    [Microsoft.Exchange.WebServices.Data.EmailMessageSchema]::ReplyTo,

    [Microsoft.Exchange.WebServices.Data.ItemSchema]::InternetMessageHeaders,

    [Microsoft.Exchange.WebServices.Data.ItemSchema]::ExtendedProperties

)

#You need to save to load more properties (this creates a draft)

$message.Save()

#Create New Xheader object

$XHeader = New-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([Microsoft.Exchange.WebServices.Data.DefaultExtendedPropertySet]::InternetHeaders,'X-CustomHeader',[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)

$message.load($propertyset)

#Set the property

$message.SetExtendedProperty($XHeader,"Sent Via EWS")

#Update the draft version

#https://docs.microsoft.com/en-us/dotnet/api/microsoft.exchange.webservices.data.conflictresolutionmode?view=exchange-ews-api

$message.Update('AlwaysOverwrite')

About mell9185

IT proffesional. Tech, video game, anime, and punk aficionado.
This entry was posted in EWS, PowerShell. Bookmark the permalink.

Leave a Reply