- Get link
- X
- Other Apps
Working with EWS Managed API via Windows PowerShell in Office 365 Explained
Exchange Web Services provides the following types of operations:
- Availability
- 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)
- Folder
- Inbox Rules (new in Exchange 2010)
- Item
- Mail Tips (new in Exchange 2010)
- Messaging Records Management
- Message Tracking (new in Exchange 2010)
- Notification
- Service Configuration (new in Exchange 2010)
- Synchronization
- Unified Messaging (new in Exchange 2010)
- User Configuration (new in Exchange 2010)
- Utility
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'
Import-Module $EWSServicePath
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.ToRecipients.Add('EwsTestingTo@LetsExchange.com')
$message.Body = "This is Test Message By Sunil Chauhan From EWS Client"
$message.Save()
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.
$message.SendAndSaveCopy()
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
########################################
#script Name = SendEmail_EWS.ps1
#Author = Sunil Chauhan
#Email= Sunilkms@gmail.com
#########################################
#Download EWS and Install from below Link
#http://www.microsoft.com/en-in/download/details.aspx?id=35371
#Web Service Path
$EWSServicePath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
#Importing WebService DLL
Import-Module $EWSServicePath
#Creating Service Object
$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
#$ExchVer = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
#$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($exchver)
#Setting up Credentials
$user = "Testuser1@LetsExchange.in"
$pass = "India@123"
$service.Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials -ArgumentList $user, $pass
#Setting up EWS URL for exchange online, if you are using it for on premise exchange you can change the same accordingly.
$EWSurl = "https://outlook.office365.com/EWS/Exchange.asmx"
$Service.URL = $EWSurl
#Setting up Email message Class
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $service
#Creating and Saving Message in Draft Folder
$message.Subject = "This Message has been Created by EWS"
$message.From = "testuser1@LetsExchange.in"
$message.ToRecipients.Add("sunilkms@LetsExchange.in")
$message.Body = "This is Test Message By Sunil Chauhan From EWS Client"
$message.Save()
#Or to Send
#$message.Save()
#or to Save and Send
#$message.SendAndSaveCopy()
Some helpful Article from MSDN.
- Get link
- X
- Other Apps
Comments
Great post Sunil.. Excellent write up.. keep rocking!!!
ReplyDeleteThe first example works for EWS 2.0, bravo
ReplyDeleteExcellent post Sunil
ReplyDeleteThis is great, is there a way to search for emails in one or more mailboxes by sender or subject to determine if they have been read?
ReplyDeleteExcellent tutorial. This is exactly what i was lookin for. Keep up the good work
ReplyDelete