We do have requirements to integrate your application with the eMail, either by sending them or by reading them (Yes! – it is possible), for this purpose, Microsoft has a cool new protocol called as the Exchange Web Services.
If you have an eMail Exchange Server 2007 and above already set up, then it is a cake walk to integrate your application with the Exchange Server eMail using EWS.
NOTE: At the time of writing this article EWS API 2.2 was the latest version.
NOTE: I am using VB .Net for this example
So lets get started!
Firstly, you’ll need to download the EWS API from the Microsoft MSDN website, using the below URL and follow the installation procedure mentioned there:
After installing the EWS API 2.2, you must now turn on your Visual Studio environment and create a new Project:
Click ‘Ok’ to create a new project.
Now lets turn our focus on the steps to reference the newly installed Exchange Web Service. We will have to manually reference them into our project. To do this:
Click the add reference menu item by right clicking the project > Add > Reference
A Reference Manager window opens up
Click Browse on the left pane and then click Browse button again from the Footer
It will open up the Windows Explorer. Navigate to the below path, which ideally the default path where the Exchange Web Services are installed.
C:\Program Files\Microsoft\Exchange\Web Services\2.2
There are two dlls that you will have to import into your project. Click Add after you select them both.
You need to keep these information handy before you follow the code below:
- Your Microsoft Exchange Server version (For example Exchange Server 2007 or Exchange Server 2010 etc.)
- eMail ID, Password and the Domain Name that you will be using to send the eMail from
- URL of your exchange web service. Ideally, it is the exchange server name. You can find your exchange server name, by going into the Account Settings. The highlighted area in the image below is the server name. So the URL of the exchange web service would be https://<Server Name>/EWS/Exchange.asmx . Clicking on the link the web service page of the Exchange should open up. It will ask you for your user name and password.
- An HTML File, for you to send an eMail (You can even send a plan text eMail.)
Now lets send an eMail using EWS!
Follow the code block below:
' We create an object of the Exchange Server. ' In the argument, I specify the Exchange Server Version. Mine is 2010. Dim service As New ExchangeService(ExchangeVersion.Exchange2010_SP2) service.Credentials = New WebCredentials(<eMail ID of the Sender>, <Password of the sender>, <Domain Name under which your Exchange Server resides>) ' This is your Exchange Web Service URL service.Url = New Uri("https://<Server Name>/EWS/Exchange.asmx") ' Set up the recepients eMail IDs Dim oEmail As New EmailMessage(service) oEmail.ToRecipients.Add(<Recipient EMail ID>) ' Read the HTML File and store it in a String. Dim sHtml As String = System.IO.File.ReadAllText("C:\index.html") ' EMail Subject, Body Block oEmail.Subject = "SUBJECT" oEmail.Body = New MessageBody(sHtml) oEmail.Body.BodyType = BodyType.HTML oEmail.Send() ' To trace the request and responses while using EWS. It helps to find out where your service is wrong. service.TraceEnabled = True service.TraceFlags = TraceFlags.All
and thats it!, this is how you can send an eMail using Microsoft Exchange Service API 2.2 web service.
You can extend the capability further, by reading the eMail messages from the inbox using the Exchange Web Service.
Let me know your comments!