private ExchangeService GetExchangeService(EmailCredentials credentials)
        {
            var url      = credentials.AccessEndPoint;
            var cacheKey = BuildCacheKey(credentials.Email);

            if (string.IsNullOrWhiteSpace(url))
            {
                url = _cacheRepository.Get <string>(cacheKey);
            }
            if (string.IsNullOrWhiteSpace(url))
            {
                var autodiscover = new AutodiscoverService(ExchangeVersion.Exchange2013_SP1)
                {
                    RedirectionUrlValidationCallback = x => true,
                    Credentials = new WebCredentials(credentials.Email, credentials.Password)
                };
                var userSettings = autodiscover.GetUsersSettings(new List <string>()
                {
                    credentials.Email
                }, UserSettingName.ExternalEwsUrl);

                var successResponse = userSettings.First(x => x.ErrorCode == AutodiscoverErrorCode.NoError);
                successResponse.TryGetSettingValue(UserSettingName.ExternalEwsUrl, out url);
                _cacheRepository.Put(cacheKey, url, new CacheRepositoryOptions());
            }
            var service =
                new ExchangeService(ExchangeVersion.Exchange2013_SP1)
            {
                Credentials = new WebCredentials(credentials.Email, credentials.Password),
                Url         = new Uri(url)
            };

            return(service);
        }
Example #2
0
        public void AutodiscoverGetUserSettings(AutodiscoverService service, string[] sUserSmtpAddresses)
        {
            string sRet = string.Empty;

            txtResults.Text = string.Empty;

            try
            {
                GetUserSettingsResponseCollection response = service.GetUsersSettings(
                    sUserSmtpAddresses.Select(addr => addr.Trim()).Where(addr => addr != ""),
                    System.Enum.GetValues(typeof(UserSettingName)) as UserSettingName[]);


                if (response.ErrorCode == AutodiscoverErrorCode.NoError)
                {
                    AutodiscoverResultForm.Show(this, response.Select(r => new AutodiscoverResultForm.AutodiscoverResult(r, service.Url)));
                }
                else
                {
                    sRet += "Response Error:\r\n\r\n";
                    sRet += "    AutodiscoverErrorCode : " + response.ErrorCode.ToString() + "\r\n";
                    sRet += "    Error Message:          " + response.ErrorMessage + "\r\n";
                }
            }
            catch (AutodiscoverLocalException oAutodiscoverLocalException)
            {
                sRet += "Caught AutodiscoverLocalException Exception:\r\n\r\n";
                sRet += "    Error Message: " + oAutodiscoverLocalException.Message + "\r\n";
                sRet += "    Inner Error Message: " + oAutodiscoverLocalException.InnerException + "\r\n";
                sRet += "    Stack Trace: " + oAutodiscoverLocalException.StackTrace + "\r\n";
                sRet += "    See: " + oAutodiscoverLocalException.HelpLink + "\r\n";
            }

            catch (AutodiscoverRemoteException oAutodiscoverRemoteException)
            {
                sRet += "Caught AutodiscoverRemoteException Exception:\r\n\r\n";
                sRet += "    Error Message: " + oAutodiscoverRemoteException.Message + "\r\n";
                sRet += "    Inner Error Message: " + oAutodiscoverRemoteException.InnerException + "\r\n";
                sRet += "    Stack Trace: " + oAutodiscoverRemoteException.StackTrace + "\r\n";
                sRet += "    See: " + oAutodiscoverRemoteException.HelpLink + "\r\n";
            }
            catch (AutodiscoverResponseException oAutodiscoverResponseException)
            {
                sRet += "Caught AutodiscoverResponseException Exception:\r\n\r\n";
                sRet += "    Error Message: " + oAutodiscoverResponseException.Message + "\r\n";
                sRet += "    Inner Error Message: " + oAutodiscoverResponseException.InnerException + "\r\n";
                sRet += "    Stack Trace: " + oAutodiscoverResponseException.StackTrace + "\r\n";
                sRet += "    See: " + oAutodiscoverResponseException.HelpLink + "\r\n";
            }
            catch (ServerBusyException srBusyException)  // 2013+
            {
                Console.WriteLine(srBusyException);
                sRet += "Caught ServerBusyException Exception:\r\n\r\n";
                sRet += "    BackOffMilliseconds: " + srBusyException.BackOffMilliseconds.ToString() + "\r\n";
                sRet += "    Error Message: " + srBusyException.Message + "\r\n";
                sRet += "    Inner Error Message: " + srBusyException.InnerException + "\r\n";
                sRet += "    Stack Trace: " + srBusyException.StackTrace + "\r\n";
                sRet += "    See: " + srBusyException.HelpLink + "\r\n";
            }
            catch (Exception ex)
            {
                sRet += "Caught Exception:\r\n\r\n";
                sRet += "    Error Message: " + ex.Message + "\r\n";
                sRet += "    Inner Error Message: " + ex.InnerException + "\r\n";
                sRet += "    Stack Trace: " + ex.StackTrace + "\r\n";
                sRet += "    See: " + ex.HelpLink + "\r\n";
            }

            txtResults.Text = sRet;
        }