none
[EWSMA] [C#] [Windows] Getting UnAuthorized connecting to Office 365 RRS feed

  • Question

  • Hello,

    I have a Windows Service that uses EWS Managed API to manage a users mailbox. Everything was working fine until all of our mailboxes where moved to Office365. Since then my service stopped working and wasn't creating any errors so I created a quick console application to test the basics of EWS.

    As of right now AutoDiscovery is not working because it cannot be found which I think we have a DNS issue, just not sure if server admin fixed it yet, so to make sure the rest of the stuff works I just hard code the URL. When I run the following code I get the following:

    The request failed. The remote server returned an error: (401) Unauthorized.

    Here is the code that I am using:

    class Program
        {
            static void Main(string[] args)
            {
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
                ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;
    
                service.Credentials = new WebCredentials("myUser", "password", "domain.com");
    
                try
                {
                    service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
    
                    SendEmail(service);
    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
    
                Console.ReadLine();
            }
    
    private static void SendEmail(ExchangeService s)
            {
                EmailMessage message = new EmailMessage(s);
                message.Subject = "Testing EWS";
                message.Body = "This is a test
                message.ToRecipients.Add("user1@domain.com");
                message.Send();
            }
    }

    From what I was reading online, with Exchange 2013 you now have to provide applications created access to use EWS. Server Admin ran the following which according to Microsoft will allow it for all applications, but I still get Unauthorized and do not know why.

    https://technet.microsoft.com/en-us/library/aa997443.aspx

    Example 2

    This example allows all client applications to use REST and EWS.

    Set-OrganizationConfig -EwsApplicationAccessPolicy EnforceBlockList -EwsBlockList $null
    

    Can anyone help me out, I am out of ideas?


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Friday, April 29, 2016 12:59 PM

Answers

  • Hi,

    Credentials for Office 365 requires the user principal name (myUser@domain.com if it is so) in office 365.

    service.Credentials = new WebCredentials("myUser@domain.com", "password");
    

    Regards,


    Désiré GOVIN Refresh IT Solutions

    • Marked as answer by clayman2 Friday, April 29, 2016 2:59 PM
    Friday, April 29, 2016 2:30 PM

All replies

  • Hi,

    Credentials for Office 365 requires the user principal name (myUser@domain.com if it is so) in office 365.

    service.Credentials = new WebCredentials("myUser@domain.com", "password");
    

    Regards,


    Désiré GOVIN Refresh IT Solutions

    • Marked as answer by clayman2 Friday, April 29, 2016 2:59 PM
    Friday, April 29, 2016 2:30 PM
  • That seems to do it, I read somewhere else that you had to use WebCredentials("user", "password" "domain"), so that is why I changed to that. I use to use service.UseDefaultCredentials = true;, but that doesn't seem to work so I will need to make some changes to my original application. Thank you for your assistance.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Friday, April 29, 2016 2:58 PM
  • UseDefaultCredentials is useful in an OnPremise environment : it uses the credentials of the current  user.

    Using WebCredentials is useful if you are using the EWS API and the current account is not a domain account.

    Have a good day,


    Désiré GOVIN Refresh IT Solutions

    Friday, April 29, 2016 3:43 PM