Return to Snippet

Revision: 21487
at December 14, 2009 16:38 by blackf0rk


Initial Code
//add the following required namespaces:
using Microsoft.Exchange.WebServices.Data;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

//create a callback required for the AutodiscoverUrl method:
private static bool ValidateRedirectionUrlCallback(string url)
{
//Validate the URL and return true to allow the redirection
return true;
}

private ExchangeService(string EmailAddress)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.Credentials = new NetworkCredential("<username>","<password>","<domain>");
service.AutodiscoverUrl(EmailAddress, ValidateRedirectionUrlCallback);
return service;
}

//example of using this service to set an appointment
public void SetAppointment()
{
ExchangeService service = ExchangeService("<email service>");
Appointment appt = new Appointment(service);
appt.Subject = "<subject>";
appt.Body = "<body>";
appt.Start = DateTime.Now;
appt.End = DateTime.Now.AddHours(2);
appt.Save(WellKnownFolderName.Calendar);
}

Initial URL

                                

Initial Description
In order for the code snippet(s) to work, you need to do the following:

1. Go download the EWS API SDK: http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1
2. Install the SDK on your machine. It will install a .dll .xml and walkthrough guide to C:\Program Files\Microsoft\Exchange...
3. Open up your VS project solution and add a reference to the installed DLL

It's important to note that you should use the service.AutodiscoverUrl instead of service.Url because the URL you use may not be the correct URL down the line (due to redirection). If you are using service.Url and you call a method and get a 405 error, switch to the AutodiscoverUrl method instead.

Initial Title
Using Exchange Web Services API

Initial Tags
web

Initial Language
C#