EWS client application with proxy class RRS feed

  • Question

  • Hi,

    I created  sample c# applications for sending email using Exchange web service managed APIs and  EWS proxy classes. My server and client applications are running in different domain. The application created with Exchange web service managed APIs are working fine. In the application created with EWS proxy class, I am getting the error The request failed with HTTP status 401: Unauthorized. In both the application I am using same authentication credentials only.

    Please suggests that what could be the problem.


    Wednesday, November 30, 2011 1:32 PM

All replies

  • It would certainly help if you post the code you are using.
     Kind regards,

    If you get your question answered, please come back and mark the reply as an answer.
    If you are helped by an answer to someone else's question, please mark it as helpful.

    Wednesday, November 30, 2011 8:55 PM
  • Hi,

    Code with managed API


                   ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);

                    service.Credentials = new WebCredentials("EmailAddress", "password");

                    //service.TraceEnabled = true;

                    service.Url = new Uri("https://domainName/EWS/Exchange.asmx");

                    EmailMessage message = new EmailMessage(service);

                    message.Subject = "Interesting";

                    message.Body = "The proposition has been considered.";




                    //Write confirmation message to console window.

                    Console.WriteLine("Message sent!");


                    // Create the CreateItem request.


    Code using proxy class
                ServicePointManager.ServerCertificateValidationCallback =
                 delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
                     // trust any certificate
                     return true;
                 // Set the version, credentials, and the Client Access server on ExchangeServiceBinding.
                 ExchangeServiceBinding esb = new ExchangeServiceBinding();
                 esb.RequestServerVersionValue = new RequestServerVersion();
                 esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;
                 esb.Credentials = new NetworkCredential("EmailAddress", "password", "domainName");
                 esb.Url = @"https://domainName/EWS/Exchange.asmx";
                 // Create the CreateItem request.
                 CreateItemType createEmailRequest = new CreateItemType();
                 // Specifiy how the e-mail will be handled.
                 createEmailRequest.MessageDisposition = MessageDispositionType.SaveOnly;
                 createEmailRequest.MessageDispositionSpecified = true;
                 // Specify the location of sent items. 
                 createEmailRequest.SavedItemFolderId = new TargetFolderIdType();
                 DistinguishedFolderIdType sentitems = new DistinguishedFolderIdType();
                 sentitems.Id = DistinguishedFolderIdNameType.sentitems;
                 createEmailRequest.SavedItemFolderId.Item = sentitems;
                 // Create the array of items.
                 createEmailRequest.Items = new NonEmptyArrayOfAllItemsType();
                 // Create a single e-mail message.
                 MessageType message = new MessageType();
                 message.Subject = "Daily Report";
                 message.Body = new BodyType();
                 message.Body.BodyType1 = BodyTypeType.Text;
                 message.Body.Value = "(1) Handled customer issues, (2) Saved the world.";
                 message.Sender = new SingleRecipientType();
                 message.Sender.Item = new EmailAddressType();
                 message.Sender.Item.EmailAddress = "SenderEmailAddress";
                 message.ToRecipients = new EmailAddressType[1];
                 message.ToRecipients[0] = new EmailAddressType();
                 message.ToRecipients[0].EmailAddress = "RecipientEmailAddress";
                 message.Sensitivity = SensitivityChoicesType.Normal;
                 // Add the message to the array of items to be created.
                 createEmailRequest.Items.Items = new ItemType[1];
                 createEmailRequest.Items.Items[0] = message;
                     // Send a CreateItem request and get the CreateItem 
                     // response.
                     CreateItemResponseType createItemResponse = esb.CreateItem(createEmailRequest);
                     ArrayOfResponseMessagesType responses = createItemResponse.ResponseMessages;
                     ResponseMessageType[] responseMessages = responses.Items;
                     // Access the response messages.
                     foreach (ResponseMessageType respMsg in responseMessages)
                         if (respMsg.ResponseClass == ResponseClassType.Error)
                             throw new Exception("Error: " + respMsg.MessageText);
                         else if (respMsg.ResponseClass == ResponseClassType.Warning)
                             throw new Exception("Warning: " + respMsg.MessageText);
                         // Check to determine whether the response message is the correct type.
                         if (respMsg is ItemInfoResponseMessageType)
                             ItemInfoResponseMessageType createItemResp = (respMsg as ItemInfoResponseMessageType);
                             ArrayOfRealItemsType aorit = createItemResp.Items;
                             foreach (ItemType item in aorit.Items)
                                 if (item is MessageType)
                                     MessageType myMessage = (item as MessageType);
                                     Console.WriteLine("Created item: " + myMessage.ItemId.Id);
                                 // TODO: Add logic to check and cast for all other types.
                 catch (Exception ex)





    • Edited by rmgs123 Thursday, December 1, 2011 4:48 AM
    Thursday, December 1, 2011 4:47 AM
  • I resolved that problem. If I pass only 2 parameters Email address and password  to  NetworkCredentials without domain it's working fine.
    Thursday, December 1, 2011 7:04 AM