{"id":281,"date":"2011-10-14T10:18:01","date_gmt":"2011-10-14T00:18:01","guid":{"rendered":"http:\/\/www.reenadu.com\/?p=281"},"modified":"2011-10-14T10:18:01","modified_gmt":"2011-10-14T00:18:01","slug":"exchange-web-services-ews-programming-with-exchange-server-2010-in-c","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=281","title":{"rendered":"Exchange Web Services (EWS) programming with Exchange Server 2010 in C#"},"content":{"rendered":"<p>I have an application to sniff email notification from Exchange Server and automatically insert data record to SQL database. Because recently our IT department upgraded MS Exchange Server to 2010 and Exchange Server 2010 does not support WebDAV any more, my robust WebDAV program is dead \ud83d\ude41<\/p>\n<p>To revive my application, I have to migrant WebDAV to EWS, because WebDAV has been replaced by Exchange Web Services (EWS). EWS is relatively new, and there is no too much information I can use. Here to share my EWS programming experience.<\/p>\n<pre lang=\"CSharp\">private void ReceiveEmail(object sender, EventArgs e)\n{\n    ExchangeServiceBinding esb = new ExchangeServiceBinding();\n    esb.Credentials = new NetworkCredential(username, password, domain);\n    ServicePointManager.ServerCertificateValidationCallback =\n      delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)\n      {\n          \/\/ trust any certificate\n          return true;\n      };\n\n    esb.Url = @\"https:\/\/exchangeserver\/EWS\/Exchange.asmx\";\n\n    FindItemType findRequest = new FindItemType();\n    findRequest.ItemShape = new ItemResponseShapeType();\n    findRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;\n\n    DistinguishedFolderIdType inbox = new DistinguishedFolderIdType();\n    inbox.Id = DistinguishedFolderIdNameType.inbox;\n    findRequest.ParentFolderIds = new []{inbox};\n    findRequest.Traversal = ItemQueryTraversalType.Shallow;\n\n\n    findRequest.ItemShape.BodyType = BodyTypeResponseType.Text;\n    findRequest.ItemShape.BodyTypeSpecified = true;\n\n    FindItemResponseType response = esb.FindItem(findRequest);\n    FindItemResponseMessageType responseMessage =\n      response.ResponseMessages.Items[0]\n          as FindItemResponseMessageType;\n    ArrayOfRealItemsType items = responseMessage.RootFolder.Item as ArrayOfRealItemsType;\n\n\n    foreach (MessageType item in items.Items)\n    {\n         MessageType msg = (MessageType) item;\n         string emailBody = GetMessageBody(esb, msg);\n         \/\/delete email\n         this.DeleteMessage(esb,msg);\n    }\n}\n\nprivate string GetMessageBody(ExchangeServiceBinding binding, MessageType message)\n{\n    string messageBody = string.Empty;\n    MessageType temp = null;\n\n    \/\/ Call GetItem on each ItemId to retrieve the\n    \/\/ item\u2019s Body property and any AttachmentIds.\n    \/\/\n    \/\/ Form the GetItem request.\n    GetItemType getItemRequest = new GetItemType();\n\n    getItemRequest.ItemShape = new ItemResponseShapeType();\n    \/\/ AllProperties on a GetItem request WILL return\n    \/\/ the message body.\n    getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;\n\n    getItemRequest.ItemIds = new ItemIdType[1];\n    getItemRequest.ItemIds[0] = (BaseItemIdType)message.ItemId;\n\n    \/\/ Here is the call to exchange.\n    GetItemResponseType getItemResponse = binding.GetItem(getItemRequest);\n\n    \/\/ We only passed in one ItemId to the GetItem\n    \/\/ request. Therefore, we can assume that\n    \/\/ we got at most one Item back.\n    ItemInfoResponseMessageType getItemResponseMessage =\n        getItemResponse.ResponseMessages.Items[0]\n        as ItemInfoResponseMessageType;\n\n    if (getItemResponseMessage != null)\n    {\n        if (getItemResponseMessage.ResponseClass ==\n            ResponseClassType.Success\n            && getItemResponseMessage.Items.Items != null\n            && getItemResponseMessage.Items.Items.Length > 0)\n            {\n                temp = (MessageType)getItemResponseMessage.Items.Items[0];\n\n                if (temp.Body != null)\n                    messageBody = temp.Body.Value;\n            }\n    }\n    return messageBody;\n}\n\nprivate void DeleteMessage(ExchangeServiceBinding binding, MessageType message)\n{\n    \/\/ Call DeleteItem on each ItemId to delete the message\n    \/\/ Form the DeleteItem request.\n    DeleteItemType delItemRequest = new DeleteItemType();\n\n    delItemRequest.ItemIds = new ItemIdType[1];\n    delItemRequest.ItemIds[0] = (BaseItemIdType)message.ItemId;\n    \n    \/\/ Here is the call to exchange.\n    DeleteItemResponseType delItemResponse = binding.DeleteItem(delItemRequest);\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have an application to sniff email notification from Exchange Server and automatically insert data record to SQL database. Because recently our IT department upgraded MS Exchange Server to 2010 and Exchange Server 2010 does not support WebDAV any more, my robust WebDAV program is dead \ud83d\ude41 To revive my application, I have to migrant &hellip; <a href=\"https:\/\/nickdu.com\/?p=281\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Exchange Web Services (EWS) programming with Exchange Server 2010 in C#&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-281","post","type-post","status-publish","format-standard","hentry","category-net","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=281"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/281\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}