exchange EWS 开发随笔二
因为微软官方的那个 WebService API 不能满足我的需求,所以我现在得改变方式来用另外的方式来掉WebService(这种方式实际上就是官方提供API文件中所使用的方法,这个其实文档里面满足了,但是文件没更新,没有相对应的方法,这个我没办法,所以才这样做啊。)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Net;
6
7
8 namespace ExchangeEWS
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 ExchangeServiceBinding esb = new ExchangeServiceBinding();
15
16 esb.Credentials = new NetworkCredential("MailAdmin", "App1234", "xxx.com");
17 esb.RequestServerVersionValue = new RequestServerVersion() { Version = ExchangeVersionType.Exchange2007_SP1 };
18 esb.ExchangeImpersonation = new ExchangeImpersonationType();
19
20 esb.ExchangeImpersonation.ConnectingSID = new ConnectingSIDType()
21 {
22 PrimarySmtpAddress = "liujunxun@lab.com"
23
24 }; ;
25
26 esb.Url = @"http://mail.lab.com/EWS/Exchange.asmx";
27
28 string id = GetRecurringMasterFromOccurrenceId(esb, new ItemIdType()
29 {
30 ChangeKey = "DwAAABYAAACmByZGocQSToqby5KUV81FAqM8fV5y",
31 Id = "AAMkADdlNTVhMTlmLWYxNWQtNDNlZi1hMTIyLTNkZTRkN2VkN2ZlNgFRAAiIy+JqlstAAEYAAAAAixAkoQ62ZEGeBtVLJhjjZwcApgcmRqHEEk6Km8uSlFfNRQFpAZtwWgAApgcmRqHEEk6Km8uSlFfNRQKjPH0b6AAAEA=="
32 }).Id;
33
34
35
36 }
37
38 /// <summary>
39 /// Deletes an attachment and returned the Id and updated change key of the
40 /// parent item
41 /// </summary>
42 /// <param name="binding">Exchange binding to use</param>
43 /// <param name="attachmentId">Attachment id to delete</param>
44 /// <returns>Id and updated change key of the parent item</returns>
45 ///
46 public static ItemIdType DeleteAttachmentAndGetItem(
47 ExchangeServiceBinding binding,
48 RequestAttachmentIdType attachmentId)
49 {
50 DeleteAttachmentType request = new DeleteAttachmentType();
51 request.AttachmentIds = new RequestAttachmentIdType[] { attachmentId };
52 DeleteAttachmentResponseType response =
53 binding.DeleteAttachment(request);
54 DeleteAttachmentResponseMessageType responseMessage =
55 response.ResponseMessages.Items[0] as
56 DeleteAttachmentResponseMessageType;
57 if (responseMessage.ResponseCode != ResponseCodeType.NoError)
58 {
59 throw new Exception("Delete attachment failed. Response code: " +
60 responseMessage.ResponseCode.ToString());
61 }
62 else
63 {
64 // Normally, we could just return the root item id from our Delete
65 // Attachment response. But I want to prove a point here, so we will
66 // call GetItem and see what happens.
67
68 // There is a bug here.
69 //
70 GetItemType getRequest = new GetItemType();
71 getRequest.ItemIds = new BaseItemIdType[] {
72 responseMessage.RootItemId };
73 getRequest.ItemShape = new ItemResponseShapeType(
74 ) { BaseShape = DefaultShapeNamesType.IdOnly };
75 GetItemResponseType getResponse = binding.GetItem(getRequest);
76 ItemInfoResponseMessageType getResponseMessage =
77 getResponse.ResponseMessages.Items[0] as
78 ItemInfoResponseMessageType;
79
80 if (getResponseMessage.ResponseCode != ResponseCodeType.NoError)
81 {
82 throw new Exception("GetItem failed. Response code: " +
83 getResponseMessage.ResponseCode.ToString());
84 }
85 else
86 {
87 return getResponseMessage.Items.Items[0].ItemId;
88 }
89 }
90 }
91
92
93
94 public static ItemIdType GetRecurringMasterFromOccurrenceId(
95 ExchangeServiceBinding binding,
96 ItemIdType idOfOccurrence)
97 {
98 // Create the RecurringMasterItemIdType instance that we will pass to GetItem.
99 //
100 RecurringMasterItemIdType recurringMasterId =
101 new RecurringMasterItemIdType();
102 recurringMasterId.OccurrenceId = idOfOccurrence.Id;
103 recurringMasterId.ChangeKey = idOfOccurrence.ChangeKey;
104
105 GetItemType getItemRequest = new GetItemType();
106 getItemRequest.ItemShape = new ItemResponseShapeType();
107 getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.IdOnly;
108
109 getItemRequest.ItemIds = new RecurringMasterItemIdType[] {
110 recurringMasterId };
111
112 // Make the call to the GetItem web method, check for success
113 // and return the item id accordingly
114 //
115 GetItemResponseType response = binding.GetItem(getItemRequest);
116 ItemInfoResponseMessageType responseMessage =
117 response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
118
119 if (responseMessage.ResponseCode != ResponseCodeType.NoError)
120 {
121 throw new Exception("GetItem failed with response code " +
122 responseMessage.ResponseCode.ToString());
123 }
124
125 return responseMessage.Items.Items[0].ItemId;
126 }
127
128
129 }
130 }
131
首先,我们建立一个基础的架构,这个呢,先打开 vs2005 ,然后建立一个工程,名字就叫做 ExchangeEWS吧。
然后 引用WebService,比如 http://mail.xxx.com/ews/Exchange.asmx ,然后看它引用过来的 cs 代码。拷贝过来,然后在吧原来的命名空间改成 ExchangeEWS。 这个是第一步完成了。
现在来完成一个功能,这个功能就是我前面日程提到的,1.无法删除附件、2.无法通过副日程得到主日程,现在贴代码。
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Net;
6
7
8 namespace ExchangeEWS
9 {
10 class Program
11 {
12 static void Main(string[] args)
13 {
14 ExchangeServiceBinding esb = new ExchangeServiceBinding();
15
16 esb.Credentials = new NetworkCredential("MailAdmin", "App1234", "xxx.com");
17 esb.RequestServerVersionValue = new RequestServerVersion() { Version = ExchangeVersionType.Exchange2007_SP1 };
18 esb.ExchangeImpersonation = new ExchangeImpersonationType();
19
20 esb.ExchangeImpersonation.ConnectingSID = new ConnectingSIDType()
21 {
22 PrimarySmtpAddress = "liujunxun@lab.com"
23
24 }; ;
25
26 esb.Url = @"http://mail.lab.com/EWS/Exchange.asmx";
27
28 string id = GetRecurringMasterFromOccurrenceId(esb, new ItemIdType()
29 {
30 ChangeKey = "DwAAABYAAACmByZGocQSToqby5KUV81FAqM8fV5y",
31 Id = "AAMkADdlNTVhMTlmLWYxNWQtNDNlZi1hMTIyLTNkZTRkN2VkN2ZlNgFRAAiIy+JqlstAAEYAAAAAixAkoQ62ZEGeBtVLJhjjZwcApgcmRqHEEk6Km8uSlFfNRQFpAZtwWgAApgcmRqHEEk6Km8uSlFfNRQKjPH0b6AAAEA=="
32 }).Id;
33
34
35
36 }
37
38 /// <summary>
39 /// Deletes an attachment and returned the Id and updated change key of the
40 /// parent item
41 /// </summary>
42 /// <param name="binding">Exchange binding to use</param>
43 /// <param name="attachmentId">Attachment id to delete</param>
44 /// <returns>Id and updated change key of the parent item</returns>
45 ///
46 public static ItemIdType DeleteAttachmentAndGetItem(
47 ExchangeServiceBinding binding,
48 RequestAttachmentIdType attachmentId)
49 {
50 DeleteAttachmentType request = new DeleteAttachmentType();
51 request.AttachmentIds = new RequestAttachmentIdType[] { attachmentId };
52 DeleteAttachmentResponseType response =
53 binding.DeleteAttachment(request);
54 DeleteAttachmentResponseMessageType responseMessage =
55 response.ResponseMessages.Items[0] as
56 DeleteAttachmentResponseMessageType;
57 if (responseMessage.ResponseCode != ResponseCodeType.NoError)
58 {
59 throw new Exception("Delete attachment failed. Response code: " +
60 responseMessage.ResponseCode.ToString());
61 }
62 else
63 {
64 // Normally, we could just return the root item id from our Delete
65 // Attachment response. But I want to prove a point here, so we will
66 // call GetItem and see what happens.
67
68 // There is a bug here.
69 //
70 GetItemType getRequest = new GetItemType();
71 getRequest.ItemIds = new BaseItemIdType[] {
72 responseMessage.RootItemId };
73 getRequest.ItemShape = new ItemResponseShapeType(
74 ) { BaseShape = DefaultShapeNamesType.IdOnly };
75 GetItemResponseType getResponse = binding.GetItem(getRequest);
76 ItemInfoResponseMessageType getResponseMessage =
77 getResponse.ResponseMessages.Items[0] as
78 ItemInfoResponseMessageType;
79
80 if (getResponseMessage.ResponseCode != ResponseCodeType.NoError)
81 {
82 throw new Exception("GetItem failed. Response code: " +
83 getResponseMessage.ResponseCode.ToString());
84 }
85 else
86 {
87 return getResponseMessage.Items.Items[0].ItemId;
88 }
89 }
90 }
91
92
93
94 public static ItemIdType GetRecurringMasterFromOccurrenceId(
95 ExchangeServiceBinding binding,
96 ItemIdType idOfOccurrence)
97 {
98 // Create the RecurringMasterItemIdType instance that we will pass to GetItem.
99 //
100 RecurringMasterItemIdType recurringMasterId =
101 new RecurringMasterItemIdType();
102 recurringMasterId.OccurrenceId = idOfOccurrence.Id;
103 recurringMasterId.ChangeKey = idOfOccurrence.ChangeKey;
104
105 GetItemType getItemRequest = new GetItemType();
106 getItemRequest.ItemShape = new ItemResponseShapeType();
107 getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.IdOnly;
108
109 getItemRequest.ItemIds = new RecurringMasterItemIdType[] {
110 recurringMasterId };
111
112 // Make the call to the GetItem web method, check for success
113 // and return the item id accordingly
114 //
115 GetItemResponseType response = binding.GetItem(getItemRequest);
116 ItemInfoResponseMessageType responseMessage =
117 response.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
118
119 if (responseMessage.ResponseCode != ResponseCodeType.NoError)
120 {
121 throw new Exception("GetItem failed with response code " +
122 responseMessage.ResponseCode.ToString());
123 }
124
125 return responseMessage.Items.Items[0].ItemId;
126 }
127
128
129 }
130 }
131