Working with EWS Managed API via Windows PowerShell in Office 365 Explained
Exchange Web Services provides the following types of operations:
- Bulk Transfer (new in Exchange 2010)
- Conversations (new in Exchange 2010)
- Delegate Management
- Exchange Store Search
- Exchange Search (new in Exchange 2010)
- Federated Sharing (new in Exchange 2010)
- Inbox Rules (new in Exchange 2010)
- Mail Tips (new in Exchange 2010)
- Messaging Records Management
- Message Tracking (new in Exchange 2010)
- Service Configuration (new in Exchange 2010)
- Unified Messaging (new in Exchange 2010)
- User Configuration (new in Exchange 2010)
For more information, you can visit the below link at MSDN.
To work with EWS First thing is to download EWS managed API 2.0 from the below URL and install.
Once the EWS API is installed you can locate Microsoft.Exchange.WebServices.dll at below default location “C:\Program Files\Microsoft\Exchange\Web Services\2.0”
You can copy DLL files to a separate Location of your choice or reference them from the default location.
Let’s now start using the EWS, so let’s first import the Microsoft.Exchange.WebServices.dll into PowerShell via running the following cmd
$EWSServicePath = C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll'
After we have imported the dll file first thing is to create a new Service Object.
$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
Now if you recall the $service array on powershell session you will see details as below.
API2.0 by default has RequestedServerVersion As Exchange2013
you can set the same to older version by running the below set of cmds
$ExchVer = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($exchver)
Now when you recall the $Service array in Powershell you will see that RequestedServerVersion is Set to Version we set as Exchange2010_sp1
EWS schemas are backward- and forward-compatible, if you create an application that targets an earlier schema version, such as Exchange 2007 SP1, your application will also work against a later schema version, such as the Exchange 2010 SP2 service, as well as Exchange Online. Because features and feature updates are driven by the schema.
We see the Credentials attribute is Empty and UseDefaultCredentials is True as we are going to mange a mailbox in Exchange Online so we will not use Default System Credentials.
So Next we need to Set the Credential to login into User Mailbox,
To Set to Credentials for EWS Client we need to use the Web Credentials Data Class Microsoft.Exchange.WebServices.Data.WebCredentials .
$user = Admin@letsExchange.in
$pass = MyPassw0rd
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $user, $pass
Now when you Recall the $Service Array we see the Credentials Attributed is populated and Use DefaultCredentials is Now Set to False
Lets go more into Details of Credentials Attribute
($service.Credentials).Credentials | fl
Now we see the username and password saved in the Client.
Now Next Lets Set the EWS Service Url
$ewsurl = "https://outlook.office365.com/EWS/Exchange.asmx"
$ServiceUrl = $ewsurl
Now if run recall the $service.url Array we can see the details as below.
Now we have completed the client Configuration So Lets test it by Creating and Sending an Email from the Powershell Client.
To Create and send a Email message we will use EmailMessage Class
So let’s create a PowerShell Object for email EmailMessage Class and assign it to service object
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
Now once you have created $message object you can see its property by running the below command
$message | Get-Member
So lets save a draft message in the Mailbox.
$mesage.Subject = “This Message has been Created by EWS”
$message.From = Ewstesting@LetsExchange.in
$message.Body = "This is Test Message By Sunil Chauhan From EWS Client"
If you do no see any Error after the last command it mean EWS client has successfully created a Message in user Mailbox in Draft Folder.
So let’s open the mailbox and verify the same.
After opening the mailbox we see the message in Draft so let’s send this message.
In this Document we have successfully setup EWS PowerShell Client and Created an Email in Draft folder and send a message successfully.
I hope you like my article please don’t forget to leave your comment and feedback.
Below I have Summarize the Script from the beginning.
You can also download the same from my Share Drive Here
Some helpful Article from MSDN.