public ConnectionConfig AutoDiscoverConnectionDetails(string email, string password) { //Try each exchange version until the correct one is found ConnectionConfig serviceConfig = new ConnectionConfig(); try { service = new ExchangeService(ExchangeVersion.Exchange2013); service.Credentials = new WebCredentials(email, password); try { service.AutodiscoverUrl(email, RedirectionUrlValidationCallback); } catch (Exception ex) { string msg = ex.Message; return null; } serviceConfig.url = service.Url.ToString(); serviceConfig.version = GetVersion(GetExchangeVersion("2013", service)); return serviceConfig; //TODO Figure out how to use Autodiscover service to get exchange version. } catch (ServiceVersionException ex1) { // Debug.DebugMessage(3, "Service Version Exception (2013): " + ex1.Message); } //Try 2010SP1 first - lowest common denominator of later versions try { service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); service.Credentials = new WebCredentials(email, password); try { service.AutodiscoverUrl(email, RedirectionUrlValidationCallback); } catch (Exception ex) { return null; } serviceConfig.url = service.Url.ToString(); serviceConfig.version = GetVersion(GetExchangeVersion("2010_SP1", service)); return serviceConfig; //TODO Figure out how to use Autodiscover service to get exchange version. } catch (ServiceVersionException ex1) { // Debug.DebugMessage(3, "Service Version Exception (2010): " + ex1.Message); } //Failed so try earlier version - 2007SP1 try { service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); service.Credentials = new WebCredentials(email, password); try { service.AutodiscoverUrl(email, RedirectionUrlValidationCallback); } catch (Exception ex) { return null; } serviceConfig.url = service.Url.ToString(); serviceConfig.version = GetVersion(GetExchangeVersion("2007_SP1", service)); return serviceConfig; } catch (ServiceVersionException ex1) { // Debug.DebugMessage(3, "Service Version Exception (2007): " + ex1.Message); } //service could not be found return null; }
public string StandardConnectionToInHouseExchanges(string username, string password, string url, string domain = null) { try { //Try each exchange version until the correct one is found string VersionResponse = null; ConnectionConfig serviceConfig = new ConnectionConfig(); WebCredentials credentials; if (domain == null) { credentials = new WebCredentials(username, password); } else { credentials = new WebCredentials(username, password, domain); } Uri URL = new Uri(url); service = new ExchangeService(ExchangeVersion.Exchange2013); service.Credentials = credentials; service.Url = URL; string foundVersion = GetExchangeVersion("2013", service); VersionResponse = foundVersion; if (foundVersion != "" && foundVersion != "404" && foundVersion != "401") return foundVersion; service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); service.Credentials = credentials; service.Url = URL; foundVersion = GetExchangeVersion("2010_SP2", service); VersionResponse = foundVersion; if (foundVersion != "" && foundVersion != "404" && foundVersion != "401") return foundVersion; service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); service.Credentials = credentials; service.Url = URL; foundVersion = GetExchangeVersion("2010_SP1", service); VersionResponse = foundVersion; if (foundVersion != "" && foundVersion != "404" && foundVersion != "401") return foundVersion; service = new ExchangeService(ExchangeVersion.Exchange2010); service.Credentials = credentials; service.Url = URL; foundVersion = GetExchangeVersion("2010", service); VersionResponse = foundVersion; if (foundVersion != "" && foundVersion != "404" && foundVersion != "401") return foundVersion; service = new ExchangeService(ExchangeVersion.Exchange2007_SP1); service.Credentials = credentials; service.Url = URL; foundVersion = GetExchangeVersion("2007_SP1", service); VersionResponse = foundVersion; if (foundVersion != "" && foundVersion != "404" && foundVersion != "401") return foundVersion; return VersionResponse; } catch (Exception ex) { // Debug.DebugMessage(2, "Error in Test Connection2: " + ex.Message); // MessageBox.Show("Error in Test Connection: " + ex.Message, "Test Connection"); return "null"; } return null; }