none
The request failed with HTTP status 401: Unauthorized when sending email from office 365(Exchange) RRS feed

  • Question

  • Hi guys, 

    I have added webreference to my project using "https://pods(some nos).outlook.com/ews/Services.wsdl". This has generated proxy objects.

    And here is my code

        ExchangeServiceBinding esb = new ExchangeServiceBinding();
                esb.Credentials = new NetworkCredential("myname", "password","name.onmicrosoft.com");
                esb.Url = @"https://pod(some nos).outlook.com/EWS/Services.wsdl";
                esb.RequestServerVersionValue = new RequestServerVersion();
                esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010_SP1;
                esb.CookieContainer = new CookieContainer();
                // Create a CreateItem request object
                CreateItemType request = new CreateItemType();

                // Setup the request:
                // Indicate that we only want to send the message. No copy will be saved.
                request.MessageDisposition = MessageDispositionType.SendOnly;
                request.MessageDispositionSpecified = true;


                // Create a message object and set its properties
                MessageType message = new MessageType();
                message.Subject = "saD";
                message.Body = new BodyType();
                message.Body.BodyType1 = BodyTypeType.Text;
                message.Body.Value = "asdsa";
                message.ToRecipients = new EmailAddressType[1];
                message.ToRecipients[0] = new EmailAddressType();
                message.ToRecipients[0].EmailAddress = "friendname@name.onmicrosoft.com";
                message.Sensitivity = SensitivityChoicesType.Normal;


                //There are some more properties in MessageType object 
                //you can set all according to your requirement
                // Construct the array of items to send
                request.Items = new NonEmptyArrayOfAllItemsType();
                request.Items.Items = new ItemType[1];
                request.Items.Items[0] = message;

                // Call the CreateItem EWS method. 
                    CreateItemResponseType response = esb.CreateItem(request);

    But I always recieve "The request failed with HTTP status 401: Unauthorized". What could be the problem?

    I have also tried replacing myname to myname.onmicrosft.com in NetworkCredential, Nothing is working.

    I have also provided all permissions to my account in office 365 account.

    And here are the headers that I receive when ths error occurs

    X-DiagInfo: HKXPRD0210CA017

    Content-Length: 0

    Date: Wed, 27 Feb 2013 12:45:26 GMT

    Server: Microsoft-IIS/7.5

    WWW-Authenticate: Basic Realm=""

    X-Powered-By: ASP.NET


    Thursday, February 28, 2013 5:00 AM

Answers

  • Thanks Glen for your reply I finally figured it out.

    Go to office 365, log in. Go to Outlook section. The url in your browser displays

    https://pod(different nos).outlook.com/owa/?realm=domain.onmicrosoft.com#modurl=0

    Copy or Put this pod(different nos) in

     esb.Url = @"https://pod(different nos).outlook.com//EWS/Exchange.asmx";

    and in  esb.Credentials = new NetworkCredential("myname@name.onmicrosoft.com", "password","name.onmicrosoft.com");

    And there we go its successful.

    I dont know why the server name given in Settings->Settings for POP or IMAP access...->POP or IMAP Settings->Server name Url is not working.

    Before this always check in https://www.testexchangeconnectivity.com/ or

    EWS Editor http://archive.msdn.microsoft.com/ewseditor.

    Have a nice coding.



    • Marked as answer by nucleons Thursday, February 28, 2013 8:46 AM
    • Edited by nucleons Friday, March 1, 2013 8:36 AM
    Thursday, February 28, 2013 8:46 AM
  • You really should be using AutoDiscover to get the URL, as you will find your mailboxes will get shifted around between datacenters and severs in Office365 so while that URL may work today it could be different tommorow. eg some like

    String MbMailbox = "user@domain.onmicrosoft.com";  
    String AutoDiscoURL = "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml";  
    NetworkCredential ncCred = new NetworkCredential("user@domain.onmicrosoft.com","password");  
    String EWSURL = null;  
    String auDisXML = "<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\"><Request>" +  
            "<EMailAddress>" + MbMailbox + "</EMailAddress>" +  
            "<AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>" +  
            "</Request>" +  
            "</Autodiscover>";  
    System.Net.HttpWebRequest adAutoDiscoRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(AutoDiscoURL);  
      
                  
    byte[] bytes = Encoding.UTF8.GetBytes(auDisXML);  
    adAutoDiscoRequest.ContentLength = bytes.Length;  
    adAutoDiscoRequest.ContentType = "text/xml";  
    adAutoDiscoRequest.Headers.Add("Translate", "F");  
    adAutoDiscoRequest.Method = "POST";  
    adAutoDiscoRequest.Credentials = ncCred;  
      
    Stream rsRequestStream = adAutoDiscoRequest.GetRequestStream();  
    rsRequestStream.Write(bytes, 0, bytes.Length);  
    rsRequestStream.Close();  
    adAutoDiscoRequest.AllowAutoRedirect = false;  
    WebResponse adResponse = adAutoDiscoRequest.GetResponse();  
    String Redirect = adResponse.Headers.Get("Location");  
    if (Redirect != null)  
    {  
        adAutoDiscoRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(Redirect);  
        adAutoDiscoRequest.ContentLength = bytes.Length;  
        adAutoDiscoRequest.ContentType = "text/xml";  
        adAutoDiscoRequest.Headers.Add("Translate", "F");  
        adAutoDiscoRequest.Method = "POST";  
        adAutoDiscoRequest.Credentials = ncCred;  
        rsRequestStream = adAutoDiscoRequest.GetRequestStream();  
    }  
    rsRequestStream.Write(bytes, 0, bytes.Length);  
    rsRequestStream.Close();  
    adResponse = adAutoDiscoRequest.GetResponse();  
    Stream rsResponseStream = adResponse.GetResponseStream();  
    XmlDocument reResponseDoc = new XmlDocument();  
    reResponseDoc.Load(rsResponseStream);  
    XmlNodeList pfProtocolNodes = reResponseDoc.GetElementsByTagName("Protocol");  
    foreach (XmlNode xnNode in pfProtocolNodes)  
    {  
        XmlNodeList adChildNodes = xnNode.ChildNodes;  
        foreach (XmlNode xnSubChild in adChildNodes)  
        {  
            switch (xnSubChild.Name)  
            {  
                case "EwsUrl": EWSURL = xnSubChild.InnerText;  
                    break;  
            }  
        }  
    }  
    ExchangeServiceBinding esExchangeServiceBinding = new ExchangeServiceBinding();  
    esExchangeServiceBinding.Credentials = ncCred;  
    esExchangeServiceBinding.Url = EWSURL;  
    esExchangeServiceBinding.CookieContainer = new CookieContainer();

    Cheers
    Glen

    • Marked as answer by nucleons Friday, March 1, 2013 8:58 AM
    Friday, March 1, 2013 5:46 AM

All replies

  • You need to use the UPN of your account and leave the domain blank eg

    esb.Credentials = new NetworkCredential("myname@name.onmicrosoft.com", "password");

    should work

    Cheers
    Glen

    Thursday, February 28, 2013 5:16 AM
  • Its returning

    The request failed with HTTP status 403: Forbidden.

    Headers:

    RequestId: b8483814-1ac7-425e-ac67-2a403d6a7be7
    X-DiagInfo: SIXPRD0210CA014
    Content-Length: 0
    Cache-Control: private
    Date: Thu, 28 Feb 2013 05:41:30 GMT
    Set-Cookie: exchangecookie=db796c8da58d4189980f0e63bf316b34; path=/
    Server: Microsoft-IIS/7.5
    X-AspNet-Version: 2.0.50727
    X-Powered-By: ASP.NET

    Thursday, February 28, 2013 5:42 AM
  • Thanks Glen for your reply I finally figured it out.

    Go to office 365, log in. Go to Outlook section. The url in your browser displays

    https://pod(different nos).outlook.com/owa/?realm=domain.onmicrosoft.com#modurl=0

    Copy or Put this pod(different nos) in

     esb.Url = @"https://pod(different nos).outlook.com//EWS/Exchange.asmx";

    and in  esb.Credentials = new NetworkCredential("myname@name.onmicrosoft.com", "password","name.onmicrosoft.com");

    And there we go its successful.

    I dont know why the server name given in Settings->Settings for POP or IMAP access...->POP or IMAP Settings->Server name Url is not working.

    Before this always check in https://www.testexchangeconnectivity.com/ or

    EWS Editor http://archive.msdn.microsoft.com/ewseditor.

    Have a nice coding.



    • Marked as answer by nucleons Thursday, February 28, 2013 8:46 AM
    • Edited by nucleons Friday, March 1, 2013 8:36 AM
    Thursday, February 28, 2013 8:46 AM
  • You really should be using AutoDiscover to get the URL, as you will find your mailboxes will get shifted around between datacenters and severs in Office365 so while that URL may work today it could be different tommorow. eg some like

    String MbMailbox = "user@domain.onmicrosoft.com";  
    String AutoDiscoURL = "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml";  
    NetworkCredential ncCred = new NetworkCredential("user@domain.onmicrosoft.com","password");  
    String EWSURL = null;  
    String auDisXML = "<Autodiscover xmlns=\"http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006\"><Request>" +  
            "<EMailAddress>" + MbMailbox + "</EMailAddress>" +  
            "<AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>" +  
            "</Request>" +  
            "</Autodiscover>";  
    System.Net.HttpWebRequest adAutoDiscoRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(AutoDiscoURL);  
      
                  
    byte[] bytes = Encoding.UTF8.GetBytes(auDisXML);  
    adAutoDiscoRequest.ContentLength = bytes.Length;  
    adAutoDiscoRequest.ContentType = "text/xml";  
    adAutoDiscoRequest.Headers.Add("Translate", "F");  
    adAutoDiscoRequest.Method = "POST";  
    adAutoDiscoRequest.Credentials = ncCred;  
      
    Stream rsRequestStream = adAutoDiscoRequest.GetRequestStream();  
    rsRequestStream.Write(bytes, 0, bytes.Length);  
    rsRequestStream.Close();  
    adAutoDiscoRequest.AllowAutoRedirect = false;  
    WebResponse adResponse = adAutoDiscoRequest.GetResponse();  
    String Redirect = adResponse.Headers.Get("Location");  
    if (Redirect != null)  
    {  
        adAutoDiscoRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(Redirect);  
        adAutoDiscoRequest.ContentLength = bytes.Length;  
        adAutoDiscoRequest.ContentType = "text/xml";  
        adAutoDiscoRequest.Headers.Add("Translate", "F");  
        adAutoDiscoRequest.Method = "POST";  
        adAutoDiscoRequest.Credentials = ncCred;  
        rsRequestStream = adAutoDiscoRequest.GetRequestStream();  
    }  
    rsRequestStream.Write(bytes, 0, bytes.Length);  
    rsRequestStream.Close();  
    adResponse = adAutoDiscoRequest.GetResponse();  
    Stream rsResponseStream = adResponse.GetResponseStream();  
    XmlDocument reResponseDoc = new XmlDocument();  
    reResponseDoc.Load(rsResponseStream);  
    XmlNodeList pfProtocolNodes = reResponseDoc.GetElementsByTagName("Protocol");  
    foreach (XmlNode xnNode in pfProtocolNodes)  
    {  
        XmlNodeList adChildNodes = xnNode.ChildNodes;  
        foreach (XmlNode xnSubChild in adChildNodes)  
        {  
            switch (xnSubChild.Name)  
            {  
                case "EwsUrl": EWSURL = xnSubChild.InnerText;  
                    break;  
            }  
        }  
    }  
    ExchangeServiceBinding esExchangeServiceBinding = new ExchangeServiceBinding();  
    esExchangeServiceBinding.Credentials = ncCred;  
    esExchangeServiceBinding.Url = EWSURL;  
    esExchangeServiceBinding.CookieContainer = new CookieContainer();

    Cheers
    Glen

    • Marked as answer by nucleons Friday, March 1, 2013 8:58 AM
    Friday, March 1, 2013 5:46 AM
  • Thank you.
    Friday, March 1, 2013 8:58 AM