There's more...

Instead of typing all of the commands required to instantiate the Exchange service object, it makes much more sense to put this code into a reusable function. Call AutoDiscover, create the email message object, and set all of the required properties. Consider the following example:

 function Send-EWSMailMessage { param( [Parameter( Position=0, Mandatory=$true, ValueFromPipelineByPropertyName=$true )] [String[]] $PrimarySmtpAddress, [Parameter( Position=1, Mandatory=$true )] [String] $From, [Parameter( Position=2, Mandatory=$true )] [String] $Subject, [Parameter( Position=3, Mandatory=$true )] [String] $Body, [Parameter( Position=4, Mandatory=$false )] [String[]] $Cc, [Parameter( Position=5, Mandatory=$false )] [String[]] $Bcc ...

Get Microsoft Exchange Server 2016 PowerShell Cookbook - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.