Search This Blog

Monday, September 27, 2010

Sample code (C#) to send mail using Exchange Web Services (EWS) Managed API hosted on a Microsoft Online Services / Exchange Online platform.

After going through lots of searches for sending out a simple mail hosted on Microsoft Online Services / Exchange Online / BPOS (Business Productivity Online Standard Suite), I came up with an answer.

We will also be able to write code to use the classic SMTP method and call the smtp server (smtp.mail.microsoftonline.com) and use the port 587 as described in their team blog (http://blogs.technet.com/b/msonline/archive/2009/09/02/using-smtp-relay-with-exchange-online.aspx).

But what-if, if we have a requirement to send out outlook meeting requests / responses OR Appointment OR Task OR any other Outlook item. Then the answer is to use EWS.

But EWS also had some confusions added to it like in many of the sites, there were suggestions to use the ExchangeServiceBinding class which is generated after adding the Web / Service reference. But when I added the web / service reference then the ExchangeServiceBinding call has been deprecated / missing.

Then finally, I found a solution to use the Exchange Web Services (EWS) Managed API which can be downloaded here (http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1).

There are lots of descriptions and how to’s for using the EWS API as well as EWS. But my approach would provide a solution to combine EWS API to access Exchange Online hosted using Microsoft Online Services / BPOS.

Below are the steps and sample code to use the EWS API. Once you download and install the API, create any C# project (Preferably using .Net Framework 3.5 and above) and copy the 2 files available at “Program Files\Microsoft\Exchange\Web Services\1.0\” Microsoft.Exchange.WebServices.dll and Microsoft.Exchange.WebServices.xml to your local folder.

And then add a reference to the above said dll to your project and add a using clause to the Microsoft.Exchange.WebServices.Data namespace.

using Microsoft.Exchange.WebServices.Data;



To access EWS by using the EWS Managed API, all you need is an instance of the ExchangeService class, as shown in the following example.



ExchangeService service = new ExchangeService();



There are only 2 options available now for the ExchangeVersion Enum. In my case it was as above OR alternatively it can be as Exchange2010.



We can set the URL of the service in one of two ways:



1. Manually, if we know the URL of Exchange Web Services or if we have previously determined it via the Autodiscover service.



2. By using the Autodiscover service.



To set the URL manually, use the following:



service.Url = new Uri("https://red001.mail.microsoftonline.com/ews/Exchange.asmx");



To set the URL by using Autodiscover, use the following:



service.AutodiscoverUrl(“Someone@example.com”);



Please change your Url according to your location.



Asia Pacific (APAC)

https://red003.mail.apac.microsoftonline.com



Europe, the Middle East, and Africa (EMEA)

https://red002.mail.emea.microsoftonline.com



North America

https://red001.mail.microsoftonline.com



Generally people recommend to use the Auto Discover service as there would be more frequent changes to the server configuration. But for a trial and error method I had used the manual Url method.



Now we come to the tricky part of the story (at-least in my case).



We will change the settings of Use Default Credentials to false.



service.UseDefaultCredentials = false;



And we will have the credentials assigned to the service through WebCredentials class. Alternatively NetworkCredentials also can be used for non web-based applications.



service.Credentials = new WebCredentials("<<user@example.com>>", "<<password>>");



This class will have an additional overloaded constructor with the domain as an optional parameter. We will leave that overloaded constructor and use the above mentioned constructor alone.



Then, we will have the message object instantiated through EmailMessage class with the overloaded constructor having service object created above as parameter.



EmailMessage message = new EmailMessage(service);





And we will add the Subject, Body & ToRecipents as mentioned below.



message.Subject = "Hello world!";
message.Body = "Sent using the EWS Managed API.";
message.ToRecipients.Add("<<someone@example.com>>");



Finally, we will send the message using the Send method.



message.Send();



If we need to send as well as save the message in the Sent Items folder then we cal use the



message.SendAndSaveCopy();



the final snippet would look something like below;




ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Url = new Uri("https://red001.mail.microsoftonline.com/ews/Exchange.asmx");
service.UseDefaultCredentials = false;
service.Credentials = new WebCredentials(“<<user@example.com>>”, “passw0rd”);

EmailMessage message = new EmailMessage(service);
message.Subject = "Hello world!";
message.Body = "Sent using the EWS Managed API.";
message.ToRecipients.Add("someone@example.com");
message.Send();




3 comments:

  1. Here is C# code for Connecting to Exchange Server using Exchange Web Service (EWS) and Send Email using Aspose.Email for .NET. I hope it will be useful. Here is he code

    static IEWSClient GetExchnageEWSClient()
    {
    const string mailboxUri = "https://outlook.office365.com/ews/exchange.asmx";
    const string domain = @"";
    const string username = @"username@ASE305.onmicrosoft.com";
    const string password = @"password";
    NetworkCredential credentials = new NetworkCredential(username, password, domain);
    IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);

    return client;
    }

    ReplyDelete
  2. I actually have been using EWS for some months in a process which checks an email store to process emails, actually attachments. The individual who wrote the process originally used smtp and it would take up to 45 seconds to process one email. By moving to EWS I moved that processing time down to milliseconds.

    I am now having issues with email timing out in several processes using smtp server. I'm wondering if moving to EWS to send the email will help eliminating these issues? Has anyone read any statistical comparisons between the two methods for sending email?

    ReplyDelete
  3. Hi, thanks for this good document for developers.
    On the net I found .NET Framework / .NET Core which is called Exchange Web Services .NET
    There are a number of use cases on the site and the API itself is well documented.
    Link is http://www.independentsoft.de/exchangewebservices/tutorial/index.html

    ReplyDelete