Exchange Web Services Managed API 1.0 入门

Exchange Web Services Managed API 1.0 release is probably around October 2009 in Microsoft Currently very little information, but added there are some good foreign material. This is an entry-level article, just want to give you a brief introduction, more specific details we can find in the MSDN inside.

Exchange provides Exchange Web Service (EWS) after 2007, it provides a new interface to access Exchange resources. EWS reach the following goals:

  • Exchange provides a unified access to resources.
  • Consistent with the outlook built logical layer (in other words, do use outlook operation, can be done with EWS)
  • Standards-based Web Service
  • Remote access

EWS collection of original and CODEX WebDAV functionality, which provides the realized schedule-related functions such as the operation is very easy. EWS is based on XML WebService SOAP protocol, which enables it to be any operating system can send remote access HTTPS requests.

Exchange2007 SP1 offers some features not included in the initial release Exchange2007, such as delegate management, public folder management, Exchange2010 EWS will contain more new features, including access to the folder associations, user access to configuration objects (these objects make developers can save and retrieve configuration by mailbox user), complete control Distribution lists, and enhance time zone support. .

EWS is very efficient Exchange resource access interface. If the auto-generated proxy class to access the EWS, such as the use of the new method in Web Service reference services in Vistual Studio, for a complex application, it is not a good method. You may still write a lot of code, because direct proxy class mapped only those agreements. And automatically generated code is very inconvenient to use (such as Vistual Studio generates some obscure property), which are so difficult to use and maintain proxy classes automatically generated.

To solve these problems the use and maintenance, we introduced Microsoft Exchange Web Services (EWS) Managed API

Best Exchange .NET development

EWS Managed API is a fully object-oriented API, just as .Net Framework class library. It is based EWS XML protocol provides a very easy to learn, use and maintenance of Exchange Web Service .NET development interface, whether beginner or experienced developer can find it in relation to the advantages of the proxy class.

Although EWS Managed API is a new API (means that we want to add a new assembly in the application), but you must understand one thing: it does not replace EWS protocol . It only achieved EWS protocol .Net developers. This means that the previous code can still be used. Regardless of the basic XML (such as Javascript programmers), or the use of a proxy class to communicate with the EWS, the previous applications are to continue to use. EWS protocol always will be a feature of Exhange, all added to the EWS protocol function, also all reflected in the EWS Manage API.

It is so simple to use

EWS Manged API does have a great deal of improvement over the proxy class. Here is the code to retrieve the folder attributes, will compare the two methods, using the first proxy class, then the use of EWS Managed API.

By proxy class to retrieve the folder properties:

  1. GetFolderType request = new GetFolderType();   
  2. request.FolderShape = new FolderResponseShapeType();   
  3. request.FolderShape.BaseShape = DefaultShapeNamesType.AllProperties;   
  4. DistinguishedFolderIdType inboxId = new DistinguishedFolderIdType();   
  5. inboxId.Id = DistinguishedFolderIdNameType.inbox;   
  6. request.FolderIds = new BaseFolderIdType[] { inboxId };   
  7. GetFolderResponseType response = serviceBinding.GetFolder(request);   
  8. FolderInfoResponseMessageType responseMessage   
  9. = response.ResponseMessages.Items[0] as FolderInfoResponseMessageType;   
  10. if (responseMessage.ResponseClass == ResponseClassType.Success)   
  11. {   
  12.    FolderType inbox = responseMessage.Folders[0] as FolderType;   
  13. }  
GetFolderType request = new GetFolderType();
request.FolderShape = new FolderResponseShapeType();
request.FolderShape.BaseShape = DefaultShapeNamesType.AllProperties;
DistinguishedFolderIdType inboxId = new DistinguishedFolderIdType();
inboxId.Id = DistinguishedFolderIdNameType.inbox;
request.FolderIds = new BaseFolderIdType[] { inboxId };
GetFolderResponseType response = serviceBinding.GetFolder(request);
FolderInfoResponseMessageType responseMessage
= response.ResponseMessages.Items[0] as FolderInfoResponseMessageType;
if (responseMessage.ResponseClass == ResponseClassType.Success)
{
   FolderType inbox = responseMessage.Folders[0] as FolderType;
}

With EWS Managed API to retrieve the folder properties:

  1. Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);  
Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);

(Oh, indeed simplify a lot of code ..)

EWS Managed API functions:

The following will appreciate the simple use of the API, let's look at some of its features:

Binding EWS

Before you start using EWS Managed API, we need to initialize an instance of a class ExchangeService. code show as below

  1. using Microsoft.Exchange.WebServices.Data;   
  2. ...   
  3. ExchangeService service = new ExchangeService();   
  4. service.Credentials = new NetworkCredential("name""pwd""domain");   
  5. service.Url = new Uri(https://myserver/EWS/Exchange.asmx);  
using Microsoft.Exchange.WebServices.Data;
...
ExchangeService service = new ExchangeService();
service.Credentials = new NetworkCredential("name", "pwd", "domain");
service.Url = new Uri(https://myserver/EWS/Exchange.asmx);

ExchangeService class has many useful methods, such as FindItems , FindAppointments , DeleteItems and AddDelegates, you can be perceived view of the function by function vs:

EWS Managed API IntelliSense

The good news is: EWS Managed API also supports Exchange 2007 SP1, you can specify the version of Exchange in the constructor ExchangeService's.

  1. ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);  
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

You can use the same version of the EWS Manged API specifies the different versions of Exchange, it can support both Exchange 2007 SP1 and Exchange 2010, of course, care must be taken if you specify the Exchange 2007 SP1 version, and 2010 related functions Exchange can not be used, if use the exception will be thrown.

Use Autodiscover

Autodiscover service is a core part of Exchange2007 and Exchange 2010 architecture. Using Autodiscover, the program can automatically get to set the mode to communicate with the server, such as Exchange Web Services in the URL.

Exchange Web Services Managed API provides a built-in Autodiscover client API, we no longer need to download Exchange Server Software Development Kit (SDK), copy the code from the Autodiscover example. Because now call Autodiscover service is just one way. code show as below:

  1. ExchangeService service = new ExchangeService();   
  2. service.Credentials = new NetworkCredential("name""pwd""domain");   
  3. service.AutodiscoverUrl([email protected]);  
ExchangeService service = new ExchangeService();
service.Credentials = new NetworkCredential("name", "pwd", "domain");
service.AutodiscoverUrl([email protected]);

Exchange 2010 will provide a new SOAP-based Autodiscover service, the following code is by way of EWS Managed API

  1. using Microsoft.Exchange.WebServices.Autodiscover;   
  2. ...   
  3. AutodiscoverService autodiscover = new AutodiscoverService("contoso.com");   
  4. autodiscover.Credentials = new NetworkCredential("name""pwd""domain");   
  5. GetUserSettingsResponse response = autodiscover.GetUserSettings(   
  6. "[email protected]",   
  7. UserSettingName.ExternalEwsUrl,   
  8. UserSettingName.InternalEwsUrl);  
using Microsoft.Exchange.WebServices.Autodiscover;
...
AutodiscoverService autodiscover = new AutodiscoverService("contoso.com");
autodiscover.Credentials = new NetworkCredential("name", "pwd", "domain");
GetUserSettingsResponse response = autodiscover.GetUserSettings(
"[email protected]",
UserSettingName.ExternalEwsUrl,
UserSettingName.InternalEwsUrl);

Processed items and folders

EWS Managed API provides a project folder and all operating functions, including object-oriented way to create, update, delete. The following code shows these features.

Create a folder in your inbox

  1. Folder folder = new Folder(service);   
  2. folder.DisplayName = "My new folder";   
  3. folder.Save(WellKnownFolderName.Inbox);  
Folder folder = new Folder(service);
folder.DisplayName = "My new folder";
folder.Save(WellKnownFolderName.Inbox);

Create and save a draft

  1. EmailMessage message =  new  EmailMessage (service);   
  2. message.Subject = "Hello from the EWS Managed API";   
  3. message.Body = "Now that's easy!";   
  4. message.ToRecipients.Add("[email protected]");   
  5. message.Save();  
EmailMessage message = new EmailMessage(service);
message.Subject = "Hello from the EWS Managed API";
message.Body = "Now that's easy!";
message.ToRecipients.Add("[email protected]");
message.Save();

Send a message and save a copy

message.SendAndSaveCopy();

Note: Save a copy is not required!

Binding and update contact

  1. Contact contact = Contact.Bind(service, new ItemId("abcdef"));   
  2. contact.CompanyName = "Fabrikam";   
  3. contact.Update(ConflictResolutionMode.AutoResolve);  
Contact contact = Contact.Bind(service, new ItemId("abcdef"));
contact.CompanyName = "Fabrikam";
contact.Update(ConflictResolutionMode.AutoResolve);

Delete Contact

  1. contact.Delete(DeleteMode.HardDelete);  
contact.Delete(DeleteMode.HardDelete);

Create a recurring meeting

  1. Appointment appointment = new Appointment(service);   
  2. appointment.Subject = "Play tennis";   
  3. appointment.Body = "Let's play tennis for an hour every Saturday at 10AM";   
  4. appointment.Start = new DateTime(2008, 12, 20, 10, 00, 00);   
  5. appointment.End = appointment.Start.AddHours(1);   
  6. appointment.RequiredAttendees.Add("[email protected]");   
  7. appointment.Recurrence = new Recurrence.WeeklyPattern(   
  8. appointment.Start.Date,   
  9. /* Every week */,   
  10. DayOfWeek.Saturday);   
  11. appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);  
Appointment appointment = new Appointment(service);
appointment.Subject = "Play tennis";
appointment.Body = "Let's play tennis for an hour every Saturday at 10AM";
appointment.Start = new DateTime(2008, 12, 20, 10, 00, 00);
appointment.End = appointment.Start.AddHours(1);
appointment.RequiredAttendees.Add("[email protected]");
appointment.Recurrence = new Recurrence.WeeklyPattern(
appointment.Start.Date,
1 /* Every week */,
DayOfWeek.Saturday);
appointment.Save(SendInvitationsMode.SendToAllAndSaveCopy);

The above example can be seen, each class has a special type of item corresponding thereto. This concept folders and the like - each folder type has a corresponding class. The image below shows the class inheritance relationship and project folder type:

Project class inheritance:

Folder class inheritance :

Use the Response object

With Exchange Web Services, Response object is able to project a series of actions, such as reply message, accept the meeting request. EWS Managed API encapsulates these concepts, using a very simple manner. Let us look at the following code:

Reply message

  1. EmailMessage message = EmailMessage.Bind(service, new ItemId("abcd"));   
  2. message.Reply("This is my reply!"true /* replyAll */);  
EmailMessage message = EmailMessage.Bind(service, new ItemId("abcd"));
message.Reply("This is my reply!", true /* replyAll */);

Send and save custom reply

  1. ResponseMessage response = message.CreateReply(true /* replyAll */);   
  2. response.BodyPrefix = "This is my customized reply!"; response.CcRecipients.Add("[email protected]");   
  3. response.SendAndSaveCopy();  
ResponseMessage response = message.CreateReply(true /* replyAll */);
response.BodyPrefix = "This is my customized reply!"; response.CcRecipients.Add("[email protected]");
response.SendAndSaveCopy();

Accept a meeting request

  1. Appointment appointment = Appointment.Bind(service, new ItemId("abcd"));   
  2. appointment.CancelMeeting();  
Appointment appointment = Appointment.Bind(service, new ItemId("abcd"));
appointment.CancelMeeting();

Canceled Meeting Request

  1. Appointment appointment = Appointment.Bind(service, new ItemId("abcd"));   
  2. appointment.CancelMeeting();  
Appointment appointment = Appointment.Bind(service, new ItemId("abcd"));
appointment.CancelMeeting();

search for

EWS Managed API provides a very comprehensive search function, the code is as follows:

Inbox displays all subfolders

  1. FindFoldersResults findResults = service.FindFolders(   
  2. WellKnownFolderName.Inbox,   
  3. new FolderView(int.MaxValue));  
FindFoldersResults findResults = service.FindFolders(
WellKnownFolderName.Inbox,
new FolderView(int.MaxValue));

Find your inbox before 10 and contains important "API" project

  1. ItemView view = new ItemView(10);   
  2. // Return only ten items.   
  3. view.SearchFilter = new SearchFilter.SearchFilterCollection(   
  4. LogicalOperator.And,   
  5. new SearchFilter.IsEqualTo(ItemSchema.Importance, Importance.High),   
  6. new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API"));   
  7. FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, view);  
ItemView view = new ItemView(10);
// Return only ten items.
view.SearchFilter = new SearchFilter.SearchFilterCollection(
LogicalOperator.And,
new SearchFilter.IsEqualTo(ItemSchema.Importance, Importance.High),
new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API"));
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, view);

Find the folder object

  1. Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);   
  2. inbox.FindItems(view);  
Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);
inbox.FindItems(view);

Create a new search folder

  1. SearchFolder searchFolder = new SearchFolder(service);   
  2. searchFolder.DisplayName = "My search folder";   
  3. searchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Inbox);   
  4. searchFolder.SearchParameters.SearchFilter = new SearchFilter.SearchFilterCollection(   
  5. LogicalOperator.And,   
  6. new SearchFilter.IsEqualTo(ItemSchema.Importance, Importance.High),   
  7. new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API"));   
  8. searchFolder.Save(WellKnownFolderName.SearchFolders);  
SearchFolder searchFolder = new SearchFolder(service);
searchFolder.DisplayName = "My search folder";
searchFolder.SearchParameters.RootFolderIds.Add(WellKnownFolderName.Inbox);
searchFolder.SearchParameters.SearchFilter = new SearchFilter.SearchFilterCollection(
LogicalOperator.And,
new SearchFilter.IsEqualTo(ItemSchema.Importance, Importance.High),
new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API"));
searchFolder.Save(WellKnownFolderName.SearchFolders);

more content

This document describes only a small part of the EWS Managed API functions, you need more, you can go to the Microsoft Exchange Team Blog or Microsoft TechNet Forums: Development lookup.

Finally, we give you provide something useful:

EWS Managed API 函数参考:Exchange Web Services Managed API Reference.

EWS Managed API 1.0 Assembly: Download (if the address is invalid, the Microsoft Download Center search for "EWS API" will be able to find a new download address)

Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2010/06/24/1764443.html

Guess you like

Origin blog.csdn.net/weixin_33851604/article/details/93496028