webservice call access denied error message

I am trying to call EWS service using jquery to send mail. I am getting access denied message. Please let me know if there are any suggestions.

   var soapMessage = '<?xml version="1.0" encoding="utf-8"?>'
   + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
   + 'xmlns:xsd="http://www.w3.org/2001/XMLSchema"'
   + 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"'
   + 'xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"'
   + 'xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages">'
   + '<soap:Header>'
    + '<RequestServerVersion Version="Exchange2010_SP2" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />'
   + '</soap:Header>'
   + '<soap:Body>'
    + '<m:CreateItem MessageDisposition="SendOnly">'
      + '<m:Items>'
       + ' <t:Message>'
       + '   <t:Subject>test</t:Subject>'
        + '  <t:Body BodyType="Text">test</t:Body>'
        + '  <t:ToRecipients>'
        + '    <t:Mailbox>'
        + '      <t:EmailAddress>myEmail@hotmail.com</t:EmailAddress>'
        + '    </t:Mailbox>'
        + '  </t:ToRecipients>'
        + '</t:Message>'
      + '</m:Items>'
     + '</m:CreateItem>'
     + '</soap:Body>'
   + '</soap:Envelope>';



        var ServiceUrl = 'https://serverName/EWS/Exchange.asmx';
        debugger;
                                                jQuery.support.cors = true;
                                                $.ajax({
                                                    url: ServiceUrl,
                                                type: "POST",
                                                dataType: "xml",
                                                data: soapMessage,
                                                contentType:"text/xml; charset=\"utf-8\"",
                                    success: processSuccess,
            error:processError
        });

        function processSuccess(data, status, req) {
            if (status == "success")
                alert("sucess");
        }

        function processError(data, status, req) {
            alert(data + " " + status);
        } 

September 29th, 2014 8:51pm

All EWS requests need to be authenticated so you need to submit some credentials that have rights to the Mailbox your trying to send as. Depending on how the server is configured generally you should be able to just use Basic Auth however it may that you server only supports NTLM so you'll need to work out how to support that (or ask the admin to enable Basic Auth). Also see http://msdn.microsoft.com/en-us/library/office/dn626019(v=exchg.150).aspx if you using 2013

Cheers
Glen

Free Windows Admin Tool Kit Click here and download it now
September 30th, 2014 4:55am

Thanks for the message. I tried with credentials but still getting the same error message.

jQuery.support.cors = true;
                                                $.ajax({
                                                    url: ServiceUrl,
                                                 type: "POST",

                                                 username:test,

                                                 password:test, 

                                                 dataType: "xml",
                                                data: soapMessage,
                                                contentType:"text/xml; charset=\"utf-8\"",
                                    success: processSuccess,
            error:processError
        });

Also I tried passing the username and password in the SOAP header.

  + '<soap:Header>'
    + '<RequestServerVersion Version="Exchange2010_SP2" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />'
    +  '<wsse:Security>'
   + '<wsse:UsernameToken>'
   +   '<wsse:Username>test</wsse:Username>'
   +   '<wsse:Password>test</wsse:Password>'
   + '</wsse:UsernameToken>'
  + '</wsse:Security>'
   + '</soap:Header>'

  • Edited by anate Tuesday, September 30, 2014 3:12 PM code added
September 30th, 2014 1:34pm

Thanks for the message. I tried with credentials but still getting the same error message.

jQuery.support.cors = true;
                                                $.ajax({
                                                    url: ServiceUrl,
                                                 type: "POST",

                                                 username:test,

                                                 password:test, 

                                                 dataType: "xml",
                                                data: soapMessage,
                                                contentType:"text/xml; charset=\"utf-8\"",
                                    success: processSuccess,
            error:processError
        });

Also I tried passing the username and password in the SOAP header.

  + '<soap:Header>'
    + '<RequestServerVersion Version="Exchange2010_SP2" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />'
    +  '<wsse:Security>'
   + '<wsse:UsernameToken>'
   +   '<wsse:Username>test</wsse:Username>'
   +   '<wsse:Password>test</wsse:Password>'
   + '</wsse:UsernameToken>'
  + '</wsse:Security>'
   + '</soap:Header>'

  • Edited by anate Tuesday, September 30, 2014 3:12 PM code added
Free Windows Admin Tool Kit Click here and download it now
September 30th, 2014 1:34pm

I'd suggest you try a few EWS requests using the EWSEditor first http://ewseditor.codeplex.com/ you can use something like fiddler to look at the headers that are being used by this app.

They other thing I would try is using the Authorization header to send basic auth eg

$.ajax({
         url: ServiceUrl,
         type: "POST", 
         dataType: "xml", 
	 headers: {
    		"Authorization": "Basic " + btoa(USERNAME + ":" + PASSWORD)
         },
         data: soapMessage, 
         contentType:"text/xml; charset=\"utf-8\"",
         success: processSuccess, 
         error:processError
});

Cheers
Glen
October 1st, 2014 3:50am

Hey anate,

I have the same problem. Did you finally manage to find a solution?

Thanks.

Free Windows Admin Tool Kit Click here and download it now
April 16th, 2015 7:57am

hello,

i have the same required, connect to EWS exchange with jquery, and my the problem is how send logn password and domain in headers .

thanks

July 15th, 2015 9:09am

what do you mean with "btoa" !!

thanks

Free Windows Admin Tool Kit Click here and download it now
July 15th, 2015 9:15am

EWS won't support CORS AFAIK (so any attempt to send a cross domain request will fail if your using OnPrem hosted within the same domain it should be okay) if your using Office365 you can use the new REST API's which will support CORS requests see https://msdn.microsoft.com/en-us/office/office365/api/mail-rest-operations

Cheers
Glen

July 16th, 2015 4:04am

i'm using EWS exchange local in the company .

this is my code (doesn't work of course ) i have exactely the same mesasge "access denied":

                var soapPacket = '<?xml version="1.0" encoding="utf-8"?>' +
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                'xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" ' +
                'xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" ' +
                'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
           '<soap:Header>' +

             '<t:RequestServerVersion Version="Exchange2010_SP2" />' +
           '</soap:Header>' +
           '<soap:Body>' +
             '<m:FindItem Traversal="Shallow">' +
               '<m:ItemShape>' +
                 '<t:BaseShape>IdOnly</t:BaseShape>' +
                 '<t:AdditionalProperties>' +
                   '<t:FieldURI FieldURI="item:Subject" />' +
                   '<t:FieldURI FieldURI="calendar:Start" />' +
                   '<t:FieldURI FieldURI="calendar:End" />' +
                 '</t:AdditionalProperties>' +
               '</m:ItemShape>' +
               '<m:CalendarView MaxEntriesReturned="5" StartDate="2015-06-20T17:30:24.127Z" EndDate="2015-07-20T17:30:24.127Z" />' +
               '<m:ParentFolderIds>' +
                 '<t:FolderId Id="AAMk" ChangeKey="AgAA" />' +
               '</m:ParentFolderIds>' +
             '</m:FindItem>' +
           '</soap:Body>' +
         '</soap:Envelope>';

                jQuery.support.cors = true;
                $.ajax({
                    crossDomain: true,
                    url: "https://company.company.com/EWS/Exchange.asmx",
                    type: "POST",
                    dataType: "xml",
                    headers: {
                        "Authorization": "Basic " + 'myloginNT' + ":" + 'pwd'
                    },
                    data: soapPacket,
                    processData: false,
                    complete: alert('complete'),
                    success: function (data) {
                        alert('ok');                      
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest);
                        alert(errorThrown);
                    }
                    ,
                    contentType: "text/xml; charset=\"utf-8\""
                });
            }

  

i don't know where i can add "domain" if it is important to send it .

thanks


  • Edited by mvc_dev 21 hours 45 minutes ago
Free Windows Admin Tool Kit Click here and download it now
July 16th, 2015 5:38am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics