c# - EWS 异常 : "Connection failed. Try later."

标签 c# asp.net iis exchangewebservices

每次我尝试调用服务器时,我都会收到错误代码:ErrorConnectionFailedConnection failed。稍后再试。 消息。

我怀疑是因为 service 的凭证是空的。虽然我不知道为什么。如果我使用我的 Windows 帐户登录名和密码手动创建凭据,它工作正常:new WebCredentials(login, password, domain);

我有一个运行良好的控制台程序(见下文),但它不在网站上。

static void Main(string[] args)
{
    var service = GetContextualService(email);
    EmailMessage email = EmailMessage.Bind(service, new ItemId(validEmailId));
    Console.ReadKey();
}

private static ExchangeService GetContextualService(string email)
{
    ExchangeService service = new ExchangeService();

    // I don't even need credentials on a Console program to make it work
    //service.Credentials = new WebCredentials(CredentialCache.DefaultCredentials);
    //service.UseDefaultCredentials = true;
    service.AutodiscoverUrl(email, RedirectionUrlValidationCallback);

    return service;
}

private static bool RedirectionUrlValidationCallback(string redirectionUrl)
{
    // The default for the validation callback is to reject the URL.
    bool result = false;

    Uri redirectionUri = new Uri(redirectionUrl);

    // Validate the contents of the redirection URL. In this simple validation
    // callback, the redirection URL is considered valid if it is using HTTPS
    // to encrypt the authentication credentials. 
    if (redirectionUri.Scheme == "https")
    {
        result = true;
    }
    return result;
}

即使使用 new WebCredentials(CredentialCache.DefaultCredentials); 在网站上使用时,它也会返回异常。 (见下文)

private ExchangeService GetContextualService(string email)
{
    ExchangeService service = new ExchangeService();

    service.Credentials = new WebCredentials(CredentialCache.DefaultCredentials);
    //service.UseDefaultCredentials = true;

    service.AutodiscoverUrl(email, RedirectionUrlValidationCallback);

    return service;
}

[HttpPost]
public List<InternetMessageHeader> GetMailHeader(JObject data)
{
    ExchangeService service = GetContextualService(data.GetValue("email").Value<string>());
    ItemId id = new ItemId(data.GetValue("mailId").Value<string>());
    // EXCEPTION BELOW
    EmailMessage email = EmailMessage.Bind(service, id);

    return email.InternetMessageHeaders.ToList();
}

为什么对 EWS 的任何调用都会返回异常?

为什么它在控制台程序上运行良好而不是在网络服务器上运行?

欢迎任何想法!

最佳答案

严格根据您发布的代码,我只能说,当您调用 AutoDiscoverUrl() 时,它可能不会与您需要与 EWS 交谈的同一台服务器交谈。虽然通常 AD 和 EWS 在 CAS 上,但(我认为)有可能获得指向其他服务器的 EWS URL。我已经有一段时间没有使用 EWS 编辑器中的代码了,但如果它不调用托管 API,它执行 AD 的方式可能会略有不同。我建议您在尝试 Bind() 之前调出 EWS URL 并查看是否可以将其粘贴到浏览器中。 (它会将您重定向到 OWA,但连接将被证明。)我还会在重定向回调中调出 AD URL,以便您知道您在与谁交谈。抱歉,我无法提供更多帮助。

关于c# - EWS 异常 : "Connection failed. Try later.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33917032/

相关文章:

ASP.NET AXD 资源在客户端被阻止?

asp.net - 通过 ssl 从 asp.net web 应用程序下载失败在 IIS7 上运行

.net - 调试 IIS 进程的 CPU 使用率过高

c# - 为什么 RijndaelManaged 加密后返回垃圾数据?

c# - 在 winform 的窗体中居中控件

.net - 将 SharePoint 2007 迁移到 2010

c# - IIS服务器找不到index.aspx文件-404错误

c# - 如何创建一个 UI 来模拟 WPF 中的 RAM 分区?

c# - 在 xaml.cs 文件的 InitializeComponent() 上出现错误

asp.net - 将我的 Web.Config 应用程序设置存储在单独的文件中时,是否总是需要 IIsreset?