续上篇 Exchange Server 2007整合与二次开发---代码篇(一)
创建联系人,这段代码来自SDK:
1 public static void CreateContact(ExchangeServiceBinding esb)
2 {
3 // Create an object of create item type.
4 CreateItemType createItemType = new CreateItemType();
5
6 // Because you are creating a contact, save the item in the Contacts folder.
7 createItemType.SavedItemFolderId = new TargetFolderIdType();
8 DistinguishedFolderIdType contactsFolder = new DistinguishedFolderIdType();
9 contactsFolder.Id = DistinguishedFolderIdNameType.contacts;
10
11 createItemType.SavedItemFolderId.Item = contactsFolder;
12
13 createItemType.Items = new NonEmptyArrayOfAllItemsType();
14 createItemType.Items.Items = new ItemType[1];
15
16 // Create a contact item type.
17 ContactItemType contactItem = new ContactItemType();
18
19 // Set the relevant properties on the contact.
20 contactItem.FileAs = "Friend A";
21
22 // Set the contact name and job information.
23 contactItem.GivenName = "Don";
24 contactItem.Surname = "Hall";
25 contactItem.CompanyName = "AdventureWorks";
26 contactItem.JobTitle = "Software Engineer";
27
28 // Set a single e-mail address for the contact.
29 contactItem.EmailAddresses = new EmailAddressDictionaryEntryType[1];
30 EmailAddressDictionaryEntryType address = new EmailAddressDictionaryEntryType();
31 address.Key = EmailAddressKeyType.EmailAddress1;
32 address.Value = "don@example.com";
33 contactItem.EmailAddresses[0] = address;
34
35 // Set a single contact physical address.
36 contactItem.PhysicalAddresses = new PhysicalAddressDictionaryEntryType[1];
37 PhysicalAddressDictionaryEntryType physicalAddress = new PhysicalAddressDictionaryEntryType();
38 physicalAddress.Key = PhysicalAddressKeyType.Home;
39 physicalAddress.Street = "1234 56 Ave NE";
40 physicalAddress.City = "La Habra Heights";
41 physicalAddress.CountryOrRegion = "United States";
42 physicalAddress.PostalCode = "98072";
43
44 contactItem.PhysicalAddresses[0] = physicalAddress;
45
46 // Set the contact telephone number.
47 contactItem.PhoneNumbers = new PhoneNumberDictionaryEntryType[1];
48 PhoneNumberDictionaryEntryType phoneEntry = new PhoneNumberDictionaryEntryType();
49 phoneEntry.Key = PhoneNumberKeyType.BusinessPhone;
50 phoneEntry.Value = "5625550100";
51
52 contactItem.PhoneNumbers[0] = phoneEntry;
53
54 createItemType.Items.Items[0] = contactItem;
55
56 // Send the request to create the contact item; receive the response.
57 CreateItemResponseType createItemResponse = esb.CreateItem(createItemType);
58
59 // Check the results of the request.
60 if (createItemResponse.ResponseMessages.Items.Length > 0 &&
61 createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
62 {
63 ItemInfoResponseMessageType responseMessage = createItemResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
64 ContactItemType contactResponse = responseMessage.Items.Items[0] as ContactItemType;
65 Console.WriteLine("Created Contact Item with Id {0} and ChangeKey {1}", contactResponse.ItemId.Id, contactResponse.ItemId.ChangeKey);
66 }
67 }
2 {
3 // Create an object of create item type.
4 CreateItemType createItemType = new CreateItemType();
5
6 // Because you are creating a contact, save the item in the Contacts folder.
7 createItemType.SavedItemFolderId = new TargetFolderIdType();
8 DistinguishedFolderIdType contactsFolder = new DistinguishedFolderIdType();
9 contactsFolder.Id = DistinguishedFolderIdNameType.contacts;
10
11 createItemType.SavedItemFolderId.Item = contactsFolder;
12
13 createItemType.Items = new NonEmptyArrayOfAllItemsType();
14 createItemType.Items.Items = new ItemType[1];
15
16 // Create a contact item type.
17 ContactItemType contactItem = new ContactItemType();
18
19 // Set the relevant properties on the contact.
20 contactItem.FileAs = "Friend A";
21
22 // Set the contact name and job information.
23 contactItem.GivenName = "Don";
24 contactItem.Surname = "Hall";
25 contactItem.CompanyName = "AdventureWorks";
26 contactItem.JobTitle = "Software Engineer";
27
28 // Set a single e-mail address for the contact.
29 contactItem.EmailAddresses = new EmailAddressDictionaryEntryType[1];
30 EmailAddressDictionaryEntryType address = new EmailAddressDictionaryEntryType();
31 address.Key = EmailAddressKeyType.EmailAddress1;
32 address.Value = "don@example.com";
33 contactItem.EmailAddresses[0] = address;
34
35 // Set a single contact physical address.
36 contactItem.PhysicalAddresses = new PhysicalAddressDictionaryEntryType[1];
37 PhysicalAddressDictionaryEntryType physicalAddress = new PhysicalAddressDictionaryEntryType();
38 physicalAddress.Key = PhysicalAddressKeyType.Home;
39 physicalAddress.Street = "1234 56 Ave NE";
40 physicalAddress.City = "La Habra Heights";
41 physicalAddress.CountryOrRegion = "United States";
42 physicalAddress.PostalCode = "98072";
43
44 contactItem.PhysicalAddresses[0] = physicalAddress;
45
46 // Set the contact telephone number.
47 contactItem.PhoneNumbers = new PhoneNumberDictionaryEntryType[1];
48 PhoneNumberDictionaryEntryType phoneEntry = new PhoneNumberDictionaryEntryType();
49 phoneEntry.Key = PhoneNumberKeyType.BusinessPhone;
50 phoneEntry.Value = "5625550100";
51
52 contactItem.PhoneNumbers[0] = phoneEntry;
53
54 createItemType.Items.Items[0] = contactItem;
55
56 // Send the request to create the contact item; receive the response.
57 CreateItemResponseType createItemResponse = esb.CreateItem(createItemType);
58
59 // Check the results of the request.
60 if (createItemResponse.ResponseMessages.Items.Length > 0 &&
61 createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success)
62 {
63 ItemInfoResponseMessageType responseMessage = createItemResponse.ResponseMessages.Items[0] as ItemInfoResponseMessageType;
64 ContactItemType contactResponse = responseMessage.Items.Items[0] as ContactItemType;
65 Console.WriteLine("Created Contact Item with Id {0} and ChangeKey {1}", contactResponse.ItemId.Id, contactResponse.ItemId.ChangeKey);
66 }
67 }
获得某账户的状态:
1public static void GetUserAvailability(ExchangeServiceBinding esb)
2 {
3 // Identify the time to compare free/busy information.
4 Duration duration = new Duration();
5 duration.StartTime = DateTime.Now;
6 duration.EndTime = DateTime.Now.AddHours(4);
7
8 // Identify the options for comparing free/busy information.
9 FreeBusyViewOptionsType fbViewOptions = new FreeBusyViewOptionsType();
10 fbViewOptions.TimeWindow = duration;
11 fbViewOptions.RequestedView = FreeBusyViewType.MergedOnly;
12 fbViewOptions.RequestedViewSpecified = true;
13 fbViewOptions.MergedFreeBusyIntervalInMinutes = 35;
14 fbViewOptions.MergedFreeBusyIntervalInMinutesSpecified = true;
15
16 MailboxData[] mailboxes = new MailboxData[1];
17 mailboxes[0] = new MailboxData();
18
19 // Identify the user mailbox to review for free/busy data.
20 EmailAddress emailAddress = new EmailAddress();
21
22 emailAddress.Address = "邮件地址";
23 emailAddress.Name = String.Empty;
24
25 mailboxes[0].Email = emailAddress;
26 mailboxes[0].ExcludeConflicts = false;
27
28 // Make the request.
29 GetUserAvailabilityRequestType request = new GetUserAvailabilityRequestType();
30
31 // Set the time zone of the request.
32 request.TimeZone = new SerializableTimeZone();
33 request.TimeZone.Bias = 480;
34 request.TimeZone.StandardTime = new SerializableTimeZoneTime();
35 request.TimeZone.StandardTime.Bias = 0;
36 request.TimeZone.StandardTime.DayOfWeek = DayOfWeekType.Sunday.ToString();
37 request.TimeZone.StandardTime.DayOrder = 1;
38 request.TimeZone.StandardTime.Month = 11;
39 request.TimeZone.StandardTime.Time = "02:00:00";
40 request.TimeZone.DaylightTime = new SerializableTimeZoneTime();
41 request.TimeZone.DaylightTime.Bias = -60;
42 request.TimeZone.DaylightTime.DayOfWeek = DayOfWeekType.Sunday.ToString();
43 request.TimeZone.DaylightTime.DayOrder = 2;
44 request.TimeZone.DaylightTime.Month = 3;
45 request.TimeZone.DaylightTime.Time = "02:00:00";
46
47 // Add the mailboxes to the request.
48 request.MailboxDataArray = mailboxes;
49
50 // Add the view options to the request.
51 request.FreeBusyViewOptions = fbViewOptions;
52
53 try
54 {
55 // Send the request and get the response.
56 GetUserAvailabilityResponseType response = esb.GetUserAvailability(request);
57
58 // Access free/busy information.
59 if (response.FreeBusyResponseArray.Length < 1)
60 {
61 throw new Exception("No free/busy response data available.");
62 }
63 else
64 {
65 foreach (FreeBusyResponseType fbrt in response.FreeBusyResponseArray)
66 {
67 if (fbrt.ResponseMessage.ResponseClass == ResponseClassType.Error)
68 {
69 Console.WriteLine(string.Format("Error: {0}", fbrt.ResponseMessage.MessageText));
70 }
71 else
72 {
73 // Show the free/busy stream.
74 FreeBusyView fbv = fbrt.FreeBusyView;
75 Console.WriteLine(string.Format("Merged free/busy data: {0}", fbv.MergedFreeBusy));
76 }
77 }
78 }
79 }
80 catch (Exception e)
81 {
82 // Perform error processing.
83 Console.WriteLine(e.Message);
84 Console.ReadLine();
85 }
86 }
2 {
3 // Identify the time to compare free/busy information.
4 Duration duration = new Duration();
5 duration.StartTime = DateTime.Now;
6 duration.EndTime = DateTime.Now.AddHours(4);
7
8 // Identify the options for comparing free/busy information.
9 FreeBusyViewOptionsType fbViewOptions = new FreeBusyViewOptionsType();
10 fbViewOptions.TimeWindow = duration;
11 fbViewOptions.RequestedView = FreeBusyViewType.MergedOnly;
12 fbViewOptions.RequestedViewSpecified = true;
13 fbViewOptions.MergedFreeBusyIntervalInMinutes = 35;
14 fbViewOptions.MergedFreeBusyIntervalInMinutesSpecified = true;
15
16 MailboxData[] mailboxes = new MailboxData[1];
17 mailboxes[0] = new MailboxData();
18
19 // Identify the user mailbox to review for free/busy data.
20 EmailAddress emailAddress = new EmailAddress();
21
22 emailAddress.Address = "邮件地址";
23 emailAddress.Name = String.Empty;
24
25 mailboxes[0].Email = emailAddress;
26 mailboxes[0].ExcludeConflicts = false;
27
28 // Make the request.
29 GetUserAvailabilityRequestType request = new GetUserAvailabilityRequestType();
30
31 // Set the time zone of the request.
32 request.TimeZone = new SerializableTimeZone();
33 request.TimeZone.Bias = 480;
34 request.TimeZone.StandardTime = new SerializableTimeZoneTime();
35 request.TimeZone.StandardTime.Bias = 0;
36 request.TimeZone.StandardTime.DayOfWeek = DayOfWeekType.Sunday.ToString();
37 request.TimeZone.StandardTime.DayOrder = 1;
38 request.TimeZone.StandardTime.Month = 11;
39 request.TimeZone.StandardTime.Time = "02:00:00";
40 request.TimeZone.DaylightTime = new SerializableTimeZoneTime();
41 request.TimeZone.DaylightTime.Bias = -60;
42 request.TimeZone.DaylightTime.DayOfWeek = DayOfWeekType.Sunday.ToString();
43 request.TimeZone.DaylightTime.DayOrder = 2;
44 request.TimeZone.DaylightTime.Month = 3;
45 request.TimeZone.DaylightTime.Time = "02:00:00";
46
47 // Add the mailboxes to the request.
48 request.MailboxDataArray = mailboxes;
49
50 // Add the view options to the request.
51 request.FreeBusyViewOptions = fbViewOptions;
52
53 try
54 {
55 // Send the request and get the response.
56 GetUserAvailabilityResponseType response = esb.GetUserAvailability(request);
57
58 // Access free/busy information.
59 if (response.FreeBusyResponseArray.Length < 1)
60 {
61 throw new Exception("No free/busy response data available.");
62 }
63 else
64 {
65 foreach (FreeBusyResponseType fbrt in response.FreeBusyResponseArray)
66 {
67 if (fbrt.ResponseMessage.ResponseClass == ResponseClassType.Error)
68 {
69 Console.WriteLine(string.Format("Error: {0}", fbrt.ResponseMessage.MessageText));
70 }
71 else
72 {
73 // Show the free/busy stream.
74 FreeBusyView fbv = fbrt.FreeBusyView;
75 Console.WriteLine(string.Format("Merged free/busy data: {0}", fbv.MergedFreeBusy));
76 }
77 }
78 }
79 }
80 catch (Exception e)
81 {
82 // Perform error processing.
83 Console.WriteLine(e.Message);
84 Console.ReadLine();
85 }
86 }
获取收件箱中邮件,有两种方式,GetItemType和FindItemType,通过FindItemType只能得到邮件的一部分属性,如标题、正文、发件人姓名等等,而像发件人地址、会议邀请邮件的会议地点、开始时间结束时间等只能通过GetItemType来获得,GetItemType能得到邮件的所有属性:
1读取会议邮件
2 public static void GetMeetingMailMessage(ExchangeServiceBinding exchangeServer)
3 {
4 DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
5 folderIDArray[0] = new DistinguishedFolderIdType();
6 folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
7
8 PathToUnindexedFieldType ptuftDisplayName = new PathToUnindexedFieldType();
9 ptuftDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName;
10
11 PathToExtendedFieldType pteftComment = new PathToExtendedFieldType();
12 pteftComment.PropertyTag = "0x3004"; // PR_COMMENT
13 pteftComment.PropertyType = MapiPropertyTypeType.String;
14
15 GetFolderType myfoldertype = new GetFolderType();
16 myfoldertype.FolderIds = folderIDArray;
17 myfoldertype.FolderShape = new FolderResponseShapeType();
18 myfoldertype.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
19 myfoldertype.FolderShape.AdditionalProperties = new BasePathToElementType[2];
20 myfoldertype.FolderShape.AdditionalProperties[0] = ptuftDisplayName;
21 myfoldertype.FolderShape.AdditionalProperties[1] = pteftComment;
22
23 GetFolderResponseType myFolder = exchangeServer.GetFolder(myfoldertype);
24
25
26 FolderInfoResponseMessageType firmtInbox =
27 (FolderInfoResponseMessageType)myFolder.ResponseMessages.Items[0];
28
29 PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
30 ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;
31
32 PathToUnindexedFieldType ptuftBody = new PathToUnindexedFieldType();
33 ptuftBody.FieldURI = UnindexedFieldURIType.itemAttachments;
34
35 PathToExtendedFieldType pteftFlagStatus = new PathToExtendedFieldType();
36 pteftFlagStatus.PropertyTag = "0x1090"; // PR_FLAG_STATUS
37 pteftFlagStatus.PropertyType = MapiPropertyTypeType.Integer;
38
39
40 FindItemType findItemRequest = new FindItemType();
41 findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
42 findItemRequest.ItemShape = new ItemResponseShapeType();
43 findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
44
45
46 findItemRequest.ParentFolderIds = new FolderIdType[1];
47 findItemRequest.ParentFolderIds[0] = firmtInbox.Folders[0].FolderId;
48
49 // 获取邮件
50 FindItemResponseType firt = exchangeServer.FindItem(findItemRequest);
51
52 // 循环迭代每一封邮件
53
54 foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
55 {
56 // 如果包含邮件,显示出来
57 if (firmtMessage.RootFolder.TotalItemsInView > 0)
58 {
59 // 循环迭代每一封邮件详细信息
60 foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
61 {
62 //会议邀请邮件
63 if (it.ItemClass == "IPM.Schedule.Meeting.Request")
64 {
65 // 通过GetItemType对象来得到邮件的正文
66 GetItemType getItemRequest = new GetItemType();
67 // 设置必要的属性
68 getItemRequest.ItemIds = new BaseItemIdType[1];
69 getItemRequest.ItemIds[0] = (BaseItemIdType)it.ItemId;
70 getItemRequest.ItemShape = new ItemResponseShapeType();
71 getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
72 getItemRequest.ItemShape.IncludeMimeContent = true;
73
74 // 获得服务器的相应
75 GetItemResponseType getItemResponse = exchangeServer.GetItem(getItemRequest);
76
77 // 得到邮件体
78 ItemInfoResponseMessageType getItemResponseMessage =
79 getItemResponse.ResponseMessages.Items[0] as
80 ItemInfoResponseMessageType;
81
82 Mail.ews.MeetingRequestMessageType meeting = getItemResponseMessage.Items.Items[0] as Mail.ews.MeetingRequestMessageType;
83
84 Console.WriteLine(meeting.Subject);
85 Console.WriteLine(meeting.From.Item.EmailAddress);
86 }
87 }
88 }
89 }
90 }
2 public static void GetMeetingMailMessage(ExchangeServiceBinding exchangeServer)
3 {
4 DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
5 folderIDArray[0] = new DistinguishedFolderIdType();
6 folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
7
8 PathToUnindexedFieldType ptuftDisplayName = new PathToUnindexedFieldType();
9 ptuftDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName;
10
11 PathToExtendedFieldType pteftComment = new PathToExtendedFieldType();
12 pteftComment.PropertyTag = "0x3004"; // PR_COMMENT
13 pteftComment.PropertyType = MapiPropertyTypeType.String;
14
15 GetFolderType myfoldertype = new GetFolderType();
16 myfoldertype.FolderIds = folderIDArray;
17 myfoldertype.FolderShape = new FolderResponseShapeType();
18 myfoldertype.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
19 myfoldertype.FolderShape.AdditionalProperties = new BasePathToElementType[2];
20 myfoldertype.FolderShape.AdditionalProperties[0] = ptuftDisplayName;
21 myfoldertype.FolderShape.AdditionalProperties[1] = pteftComment;
22
23 GetFolderResponseType myFolder = exchangeServer.GetFolder(myfoldertype);
24
25
26 FolderInfoResponseMessageType firmtInbox =
27 (FolderInfoResponseMessageType)myFolder.ResponseMessages.Items[0];
28
29 PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
30 ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;
31
32 PathToUnindexedFieldType ptuftBody = new PathToUnindexedFieldType();
33 ptuftBody.FieldURI = UnindexedFieldURIType.itemAttachments;
34
35 PathToExtendedFieldType pteftFlagStatus = new PathToExtendedFieldType();
36 pteftFlagStatus.PropertyTag = "0x1090"; // PR_FLAG_STATUS
37 pteftFlagStatus.PropertyType = MapiPropertyTypeType.Integer;
38
39
40 FindItemType findItemRequest = new FindItemType();
41 findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
42 findItemRequest.ItemShape = new ItemResponseShapeType();
43 findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
44
45
46 findItemRequest.ParentFolderIds = new FolderIdType[1];
47 findItemRequest.ParentFolderIds[0] = firmtInbox.Folders[0].FolderId;
48
49 // 获取邮件
50 FindItemResponseType firt = exchangeServer.FindItem(findItemRequest);
51
52 // 循环迭代每一封邮件
53
54 foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
55 {
56 // 如果包含邮件,显示出来
57 if (firmtMessage.RootFolder.TotalItemsInView > 0)
58 {
59 // 循环迭代每一封邮件详细信息
60 foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
61 {
62 //会议邀请邮件
63 if (it.ItemClass == "IPM.Schedule.Meeting.Request")
64 {
65 // 通过GetItemType对象来得到邮件的正文
66 GetItemType getItemRequest = new GetItemType();
67 // 设置必要的属性
68 getItemRequest.ItemIds = new BaseItemIdType[1];
69 getItemRequest.ItemIds[0] = (BaseItemIdType)it.ItemId;
70 getItemRequest.ItemShape = new ItemResponseShapeType();
71 getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
72 getItemRequest.ItemShape.IncludeMimeContent = true;
73
74 // 获得服务器的相应
75 GetItemResponseType getItemResponse = exchangeServer.GetItem(getItemRequest);
76
77 // 得到邮件体
78 ItemInfoResponseMessageType getItemResponseMessage =
79 getItemResponse.ResponseMessages.Items[0] as
80 ItemInfoResponseMessageType;
81
82 Mail.ews.MeetingRequestMessageType meeting = getItemResponseMessage.Items.Items[0] as Mail.ews.MeetingRequestMessageType;
83
84 Console.WriteLine(meeting.Subject);
85 Console.WriteLine(meeting.From.Item.EmailAddress);
86 }
87 }
88 }
89 }
90 }