EWS(Exchange  Web Service)是微软的下一代Exchange客户端通信协议。微软已经不提倡使用MAPI来访问Exchange。下面列举一个EWS的例子。该例子访问Exchange服务器,列出收件箱里的邮件的标题。

namespace ConsoleApplication1_0616
{
    class Program
    {
        static void Main(string[] args)
        {
            new Program().start();
        }
        public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }

        void start()
        {
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);

            // Identify the service binding and the user.
            ExchangeServiceBinding service = new ExchangeServiceBinding();

            service.RequestServerVersionValue = new RequestServerVersion();
            service.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010;
            service.Credentials = new NetworkCredential("xxxxx", "p@ssw0rd", "xxxx.local");
            service.Url = @"https://192.168.0.33/EWS/Exchange.asmx";

            FindItemType request = new FindItemType();
            request.ItemShape = new ItemResponseShapeType();
            request.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
            DistinguishedFolderIdType folder = new DistinguishedFolderIdType();
            folder.Id = DistinguishedFolderIdNameType.inbox;

            request.ParentFolderIds = new BaseFolderIdType[] { folder };
            request.Traversal = ItemQueryTraversalType.Shallow;

            FindItemResponseType response = service.FindItem(request);
            FindItemResponseMessageType responseMessage =
                response.ResponseMessages.Items[0] as FindItemResponseMessageType;

            ItemType[] messages = (responseMessage.RootFolder.Item as ArrayOfRealItemsType).Items;
            foreach (ItemType it in messages) {

                Console.WriteLine(it.Subject);
            }
        }

    }
}