简体   繁体   English

错误远程服务器返回错误:(401)未经授权

[英]Error The remote server returned an error: (401) Unauthorized

I am trying to get emails from my shared outlook exchange inbox, but unfortunately it is not working because of this error:我正在尝试从我的共享 outlook 交换收件箱中获取电子邮件,但不幸的是,由于此错误,它无法正常工作:

The request failed.请求失败。 The remote server returned an error: (401) Unauthorized远程服务器返回错误:(401)未经授权

This is the code I am currently using.这是我目前正在使用的代码。 It is preety simple:这很简单:

static void Main(string[] args)
{
    ExchangeService exchange = new ExchangeService();
    try
    {
        Console.WriteLine("Registering Exchange connection");

        exchange.Credentials = new WebCredentials("user", "password", "domain");
    }
    catch
    {
        Console.WriteLine("new ExchangeService failed. Press enter to exit:");
        return;
    }

    exchange.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");

    try
    {
        Console.WriteLine("Reading mail");

        // Read 100 mails
        foreach (EmailMessage email in exchange.FindItems(WellKnownFolderName.Inbox, new ItemView(100)))
        {
            Console.WriteLine(email);

        }

        Console.WriteLine("Exiting");
    }
    catch (Exception e)
    {
        Console.WriteLine("An error has occured. \n:" + e.Message);
    }
}

Basic authentication in now disabled by default in Office365 so you should avoid using it. Office365 现在默认禁用基本身份验证,因此您应该避免使用它。 Look at implementing oAuth in your see code https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth查看在代码 https 中实现oAuth://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using- oauth

If your writing a new application you may also want to consider using the Graph API rather the EWS (which is for legacy,OnPrem or Migration apps) if all the mailboxes you need to access are in the Cloud.如果您正在编写一个新应用程序,如果您需要访问的所有邮箱都在云中,您可能还需要考虑使用 Graph API 而不是 EWS(用于遗留、OnPrem 或迁移应用程序)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM