You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

O365用户通过EWS访问本地公共文件夹仍报错:There are no public folder servers available

问题描述
  • 场景:本地部署公共文件夹(PubFolder on Prem),用户处于O365环境
  • 异常:调用ewsClient.FindFolders(WellKnownFolderName.PublicFoldersRoot, new FolderView(10))时抛出

Microsoft.Exchange.WebServices.Data.ServiceResponseException: 'There are no public folder servers available.'

  • 前置情况:已按文档要求获取并添加路由头,Token获取、X头获取流程均正常
相关代码
static async System.Threading.Tasks.Task Test3()
{
    string ClientId = ConfigurationManager.AppSettings["appId"];
    string TenantId = ConfigurationManager.AppSettings["tenantId"];
    string secret = ConfigurationManager.AppSettings["clientSecret"];
    string uMbox = ConfigurationManager.AppSettings["userId"];
    string uPwd = ConfigurationManager.AppSettings["userPWD"];

// Using Microsoft.Identity.Client 4.22.0
    var cca = ConfidentialClientApplicationBuilder
        .Create(ClientId)
        .WithClientSecret(secret)
        .WithTenantId(TenantId)
        .Build();

    var ewsScopes = new string[] { "https://outlook.office365.com/.default" };

    try
    {
        var authResult = await cca.AcquireTokenForClient(ewsScopes)
            .ExecuteAsync();

        // Configure the ExchangeService with the access token
        var ewsClient = new ExchangeService();
        ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
        ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
        ewsClient.ImpersonatedUserId =
            new ImpersonatedUserId(ConnectingIdType.SmtpAddress, uMbox);


        AutodiscoverService autodiscoverService = GetAutodiscoverService(uMbox, uPwd);

        GetUserSettingsResponse userResponse = GetUserSettings(autodiscoverService, uMbox, 3, UserSettingName.PublicFolderInformation, UserSettingName.InternalRpcClientServer);
        string pfAnchorHeader= userResponse.Settings[UserSettingName.PublicFolderInformation].ToString();
        string pfMailboxHeader = userResponse.Settings[UserSettingName.InternalRpcClientServer].ToString(); ;

        // Make an EWS call
        var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));
        foreach (var folder in folders)
        {
            Console.WriteLine($"Folder: {folder.DisplayName}");
        }

        //get Public folder root
        //Include x-anchormailbox header
        Console.WriteLine("X-AnchorMailbox value for public folder hierarchy requests: {0}", pfAnchorHeader);
        Console.WriteLine("X-PublicFolderMailbox value for public folder hierarchy requests: {0}", pfMailboxHeader);

        //var test3 = GetMailboxGuidAddress(ewsClient, pfAnchorHeader, pfMailboxHeader, uMbox);

        ewsClient.HttpHeaders.Add("X-AnchorMailbox", userResponse.Settings[UserSettingName.PublicFolderInformation].ToString());
        //ewsClient.HttpHeaders.Add("X-AnchorMailbox", "SharedPublicFolder@contoso.com");
        ewsClient.HttpHeaders.Add("X-PublicFolderMailbox", userResponse.Settings[UserSettingName.InternalRpcClientServer].ToString());

        try
        {
            var pubfolders = ewsClient.FindFolders(WellKnownFolderName.PublicFoldersRoot, new FolderView(10));
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            throw;
        }
        
        foreach (var folder in folders)
        {
            Console.WriteLine($"Folder: {folder.DisplayName}");
        }

    }
    catch (MsalException ex)
    {
        Console.WriteLine($"Error acquiring access token: {ex}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex}");
    }

    if (System.Diagnostics.Debugger.IsAttached)
    {
        Console.WriteLine("Hit any key to exit...");
        Console.ReadKey();
    }
}

public static AutodiscoverService GetAutodiscoverService(string username, string pwd)
{
    AutodiscoverService adAutoDiscoverService = new AutodiscoverService();
    adAutoDiscoverService.Credentials = new WebCredentials(username, pwd);
    adAutoDiscoverService.EnableScpLookup = true;
    adAutoDiscoverService.RedirectionUrlValidationCallback = RedirectionUrlValidationCallback;
    adAutoDiscoverService.PreAuthenticate = true;
    adAutoDiscoverService.TraceEnabled = true;
    adAutoDiscoverService.KeepAlive = false;
    return adAutoDiscoverService;
}

public static GetUserSettingsResponse GetUserSettings(
         AutodiscoverService service,
         string emailAddress,
         int maxHops,
         params UserSettingName[] settings)
{
    Uri url = null;
    GetUserSettingsResponse response = null;

    for (int attempt = 0; attempt < maxHops; attempt++)
{
        service.Url = url;
        service.EnableScpLookup = (attempt < 2);

        response = service.GetUserSettings(emailAddress, settings);

        if (response.ErrorCode == AutodiscoverErrorCode.RedirectAddress)
        {
            url = new Uri(response.RedirectTarget);
        }
        else if (response.ErrorCode == AutodiscoverErrorCode.RedirectUrl)
        {
            url = new Uri(response.RedirectTarget);
        }
        else
        {
            return response;
        }
    }

    throw new Exception("No suitable Autodiscover endpoint was found.");
}

内容的提问来源于stack exchange,提问作者RobG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 19:15:38