简体   繁体   中英

Why accessing mailbox via Exchange Web Service EWS API fails randomly?

Hi I am stuck badly in this issue while accessing outloook Office 365 mailbox inbox using EWS API. I am using SSIS package Script Task to run code for connecting to emailID on Office365 but it just randomly fails or runs successfully. I have implemented it as below

protected void ConnectToExchangeServer(string emailID, stringpassword)
{
    ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;

    exchange = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
    //exchange.Credentials = new WebCredentials("USERNAME", "PASSWORD", "DOMAIN");

    exchange.Credentials = new WebCredentials(emailID, password);

    //Hair splitting problem here in autodiscoverurl, sometimes runs successfully , 
    //connects to inbox and reads mail and sometimes fails randomly 
    //throwing error Autodiscover could not find location.

    exchange.AutodiscoverUrl(emailID, RedirectionUrlValidationCallback);

    //exchange.Credentials = new WebCredentials("USERNAME", "PASSWORD", "DOMAIN");

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

 }

  private static bool RedirectionUrlValidationCallback(){...}

  private static bool CertificateValidationCallBack(object sender,
        System.Security.Cryptography.X509Certificates.X509Certificate certificate,
        System.Security.Cryptography.X509Certificates.X509Chain chain,
        System.Net.Security.SslPolicyErrors sslPolicyErrors){..}

What could be the reason for this issue ? I don't have network administration knowledge so please guide me how to check for the issue where my request is being rejected randonly ?

Is the error you're getting something like "The Autodiscovery service could not be located?" Unfortunately, that's a catch-all error that can mask the underlying error. If it's intermittent, ie same SMTP+credentials works sometimes and not others, then it may be a timeout situation. Or, if you are actually calling this same function on multiple threads in your program, once you get into about 20 simultaneous calls, EWS will start to throttle you with transient errors.

The only way to know what's going wrong is to enable tracing on the ExchangeService object, and define a handler to print out the XML that comes out of the AutodiscoverUrl call steps. It's easy enough to find how on the web, and not too bad to add. Good thing to be able to enable dynamically if this is going to be a production-class application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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