EWS Programming # 1 : Creating my first Exchange Web Services client application in C#.Net
Please find my first web service client that i tried couple of months earlier that creates an e-mail item in the Sent Items folder. You can use Microsoft Visual Studio 2005 to create a simple Exchange Web Services client application like this.
1: using System;
2: using System.Net;
3: using System.Security.Cryptography.X509Certificates;
4: using System.Net.Security;
5:
6: // The ExchangeWebServices namespace was created by using Visual Studio // 2005 and the Add Web Reference wizard.
7: using ExchangeWebServices;
8:
9: namespace Devas_First_WebService
10: {
11: partial class Program
12: {
13: static void Main(string[] args)
14: {
15: ServicePointManager.ServerCertificateValidationCallback =
16: delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
17: {
18: // Replace this line with code to validate server
19: // certificate.
20: return true;
21: };
22:
23: // Identify the service binding and the user.
24: ExchangeServiceBinding esb = new ExchangeServiceBinding();
25: // Provide the Network credentials - username, password, domain
26: esb.Credentials = new NetworkCredential("<username>", "<password>", "<domain>");
27: // Provide the Exchange Web Service (URL)
28: esb.Url = @"https://<FQDN>/EWS/Exchange.asmx";
29:
30: // Create the CreateItem request.
31: CreateItemType createEmailRequest = new CreateItemType();
32:
33: // Specifiy how the e-mail will be handled.
34: createEmailRequest.MessageDisposition = MessageDispositionType.SaveOnly;
35: createEmailRequest.MessageDispositionSpecified = true;
36:
37: // Specify the location of sent items.
38: createEmailRequest.SavedItemFolderId = new TargetFolderIdType();
39: DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType();
40: sentitems.Id = DistinguishedFolderIdNameType.sentitems;
41: createEmailRequest.SavedItemFolderId.Item = sentitems;
42:
43: // Create the array of items.
44: createEmailRequest.Items = new NonEmptyArrayOfAllItemsType();
45:
46: // Create a single e-mail message.
47: MessageType message = new MessageType();
48: message.Subject = "Daily Report";
49: message.Body = new BodyType();
50: message.Body.BodyType1 = BodyTypeType.Text;
51: message.Body.Value = "(1) Handled customer issues, (2) Saved the world.";
52: message.Sender = new SingleRecipientType();
53: message.Sender.Item = new EmailAddressType();
54: message.Sender.Item.EmailAddress = "user1@example.com";
55: message.ToRecipients = new EmailAddressType[1];
56: message.ToRecipients[0] = new EmailAddressType();
57: message.ToRecipients[0].EmailAddress = "user2@example.com";
58: message.Sensitivity = SensitivityChoicesType.Normal;
59:
60: // Add the message to the array of items to be created.
61: createEmailRequest.Items.Items = new ItemType[1];
62: createEmailRequest.Items.Items[0] = message;
63:
64: try
65: {
66: // Send a CreateItem request and get the CreateItem
67: // response.
68: CreateItemResponseType createItemResponse = esb.CreateItem(createEmailRequest);
69: ArrayOfResponseMessagesType responses = createItemResponse.ResponseMessages;
70: ResponseMessageType[] responseMessages = responses.Items;
71:
72: // Access the response messages.
73: foreach (ResponseMessageType respMsg in responseMessages)
74: {
75: if (respMsg.ResponseClass == ResponseClassType.Error)
76: {
77: throw new Exception("Error: " + respMsg.MessageText);
78: }
79: else if (respMsg.ResponseClass == ResponseClassType.Warning)
80: {
81: throw new Exception("Warning: " + respMsg.MessageText);
82: }
83:
84: // Check to determine whether the response message is the correct type.
85: if (respMsg is ItemInfoResponseMessageType)
86: {
87: ItemInfoResponseMessageType createItemResp = (respMsg as ItemInfoResponseMessageType);
88: ArrayOfRealItemsType aorit = createItemResp.Items;
89:
90: foreach (ItemType item in aorit.Items)
91: {
92: if (item is MessageType)
93: {
94: MessageType myMessage = (item as MessageType);
95: Console.WriteLine("Created item: " + myMessage.ItemId.Id);
96: Console.ReadLine();
97: }
98: // If needed add the logic to check and cast for all other types.
99: }
100: }
101: }
102: }
103: catch (Exception ex)
104: {
105: Console.WriteLine(ex.Message);
106: Console.ReadLine();
107: }
108: }
109: }
110: }
Reference: Creating an Exchange Web Services Client Application