EWS例外:"连接失败。稍后再试。"

时间:2015-11-25 12:52:48

标签: c# asp.net iis exchangewebservices

每当我尝试呼叫服务器时,都会收到错误代码:ErrorConnectionFailed并带有Connection failed. Try later.消息。

我怀疑这是因为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的任何调用都会给我一个异常?

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

欢迎任何想法!

1 个答案:

答案 0 :(得分:0)

严格基于您发布的代码,我可以说的是,当您调用AutoDiscoverUrl()时,它可能与您需要与EWS交谈的同一服务器进行通信。虽然通常AD和EWS都在CAS上,但我(我认为)可以获得指向其他服务器的EWS URL。我有一段时间没有参与EWS编辑器中的代码,但如果它没有调用Managed API,它可能会稍微改变AD。我建议您在尝试Bind()之前调用EWS URL,看看是否可以将其粘贴到浏览器中。 (它会重定向你OWA,但连接将被证明。)我还会在重定向回调中调出AD URL,以便您知道与谁通话。对不起,我无法提供更多帮助。