diff --git a/src/account-setup-eplugin/exchange-ews-account-setup.c b/src/account-setup-eplugin/exchange-ews-account-setup.c
index ff553dd..3afb8e0 100644
--- a/src/account-setup-eplugin/exchange-ews-account-setup.c
+++ b/src/account-setup-eplugin/exchange-ews-account-setup.c
@@ -167,6 +167,9 @@ validate_credentials (GtkWidget *widget, struct _AutoDiscCallBackData *cbdata)
 	EConfig *config = cbdata->config;
 	EMConfigTargetSettings *target_account = (EMConfigTargetSettings *)(config->target);
 	gchar *password = NULL;
+	CamelEwsSettings *ews_settings;
+
+	ews_settings = CAMEL_EWS_SETTINGS (target_account->storage_settings);
 
 	password = get_password (target_account);
 	/*Can there be a account without password ?*/
@@ -174,7 +177,9 @@ validate_credentials (GtkWidget *widget, struct _AutoDiscCallBackData *cbdata)
 		e_ews_autodiscover_ws_url (
 			autodiscover_callback, cbdata,
 			target_account->email_address,
-			password);
+			password,
+			camel_ews_settings_get_hosturl (ews_settings),
+			camel_network_settings_get_user (CAMEL_NETWORK_SETTINGS (ews_settings)));
 	}
 	g_free (password);
 }
diff --git a/src/server/e-ews-connection.c b/src/server/e-ews-connection.c
index 8e02db3..9a164ba 100644
--- a/src/server/e-ews-connection.c
+++ b/src/server/e-ews-connection.c
@@ -1000,7 +1000,7 @@ struct _autodiscover_data {
 	EEwsConnection *cnc;
 	xmlOutputBuffer *buf;
 	GSimpleAsyncResult *simple;
-	SoupMessage *msgs[2];
+	SoupMessage *msgs[4];
 	EEwsAutoDiscoverCallback cb;
 	gpointer cbdata;
 };
@@ -1054,11 +1054,11 @@ autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpointer data)
 	int idx;
 	gboolean success = FALSE;
 
-	for (idx = 0; idx < 2; idx++) {
+	for (idx = 0; idx < 4; idx++) {
 		if (ad->msgs[idx] == msg)
 			break;
 	}
-	if (idx == 2) {
+	if (idx == 4) {
 		/* We already got removed (cancelled). Do nothing */
 		return;
 	}
@@ -1137,7 +1137,7 @@ autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpointer data)
 	}
 
 	/* We have a good response; cancel all the others */
-	for (idx = 0; idx < 2; idx++) {
+	for (idx = 0; idx < 4; idx++) {
 		if (ad->msgs[idx]) {
 			SoupMessage *m = ad->msgs[idx];
 			ad->msgs[idx] = NULL;
@@ -1152,7 +1152,7 @@ autodiscover_response_cb (SoupSession *session, SoupMessage *msg, gpointer data)
 	return;
 
 failed:
-	for (idx = 0; idx < 2; idx++) {
+	for (idx = 0; idx < 4; idx++) {
 		if (ad->msgs[idx]) {
 			/* There's another request outstanding.
 			   Hope that it has better luck. */
@@ -1216,12 +1216,13 @@ e_ews_get_msg_for_url (const gchar *url, xmlOutputBuffer *buf)
 
 void
 e_ews_autodiscover_ws_url (EEwsAutoDiscoverCallback cb, gpointer cbdata,
-			   const gchar *email, const gchar *password)
+			   const gchar *email, const gchar *password,
+			   const gchar *ews_url, const gchar *username)
 {
 	struct _autodiscover_data *ad;
 	xmlOutputBuffer *buf;
 	GError *error = NULL;
-	gchar *url;
+	gchar *url1, *url2, *url3, *url4;
 	gchar *domain;
 	xmlDoc *doc;
 	EEwsConnection *cnc;
@@ -1245,11 +1246,31 @@ e_ews_autodiscover_ws_url (EEwsAutoDiscoverCallback cb, gpointer cbdata,
 	xmlNodeDumpOutput(buf, doc, xmlDocGetRootElement(doc), 0, 1, NULL);
 	xmlOutputBufferFlush(buf);
 
-	url = g_strdup_printf("https://%s/autodiscover/autodiscover.xml", domain);
+	url1 = NULL;
+	url2 = NULL;
+	url3 = NULL;
+	url4 = NULL;
+	if (ews_url) {
+		SoupURI *uri = soup_uri_new (ews_url);
+
+		if (uri) {
+			url1 = g_strdup_printf ("https://%s/autodiscover/autodiscover.xml", soup_uri_get_host (uri));
+			url2 = g_strdup_printf ("https://autodiscover.%s/autodiscover/autodiscover.xml", soup_uri_get_host (uri));
+			soup_uri_free (uri);
+		}
+	} 
+
+	url3 = g_strdup_printf ("https://%s/autodiscover/autodiscover.xml", domain);
+	url4 = g_strdup_printf ("https://autodiscover.%s/autodiscover/autodiscover.xml", domain);
 
-	/* FIXME: Get username from config; don't assume same as email */
-	cnc = e_ews_connection_new (url, email, password, NULL, NULL, &error);
+	cnc = e_ews_connection_new (url3, (username && *username) ? username : email, password, NULL, NULL, &error);
 	if (!cnc) {
+		g_free (url1);
+		g_free (url2);
+		g_free (url3);
+		g_free (url4);
+		xmlOutputBufferClose (buf);
+		xmlFreeDoc (doc);
 	err:
 		cb (NULL, cbdata, error);
 		return;
@@ -1270,12 +1291,10 @@ e_ews_autodiscover_ws_url (EEwsAutoDiscoverCallback cb, gpointer cbdata,
 	ad->buf = buf;
 	ad->simple = g_simple_async_result_new (G_OBJECT (cnc), autodiscover_done_cb,
 					    ad, e_ews_autodiscover_ws_url);
-	ad->msgs[0] = e_ews_get_msg_for_url (url, buf);
-	g_free (url);
-
-	url = g_strdup_printf("https://autodiscover.%s/autodiscover/autodiscover.xml", domain);
-	ad->msgs[1] = e_ews_get_msg_for_url (url, buf);
-	g_free (url);
+	ad->msgs[0] = url1 ? e_ews_get_msg_for_url (url1, buf) : NULL;
+	ad->msgs[1] = url2 ? e_ews_get_msg_for_url (url2, buf) : NULL;
+	ad->msgs[2] = url3 ? e_ews_get_msg_for_url (url3, buf) : NULL;
+	ad->msgs[3] = url4 ? e_ews_get_msg_for_url (url4, buf) : NULL;
 
 	/* These have to be submitted only after they're both set in ad->msgs[]
 	   or there will be races with fast completion */
@@ -1283,10 +1302,18 @@ e_ews_autodiscover_ws_url (EEwsAutoDiscoverCallback cb, gpointer cbdata,
 				    autodiscover_response_cb, ad);
 	soup_session_queue_message (cnc->priv->soup_session, ad->msgs[1],
 				    autodiscover_response_cb, ad);
+	soup_session_queue_message (cnc->priv->soup_session, ad->msgs[2],
+				    autodiscover_response_cb, ad);
+	soup_session_queue_message (cnc->priv->soup_session, ad->msgs[3],
+				    autodiscover_response_cb, ad);
 
 	g_object_unref (cnc); /* the GSimpleAsyncResult holds it now */
 
 	xmlFreeDoc (doc);
+	g_free (url1);
+	g_free (url2);
+	g_free (url3);
+	g_free (url4);
 }
 
 
diff --git a/src/server/e-ews-connection.h b/src/server/e-ews-connection.h
index 03d4860..3c2a26f 100644
--- a/src/server/e-ews-connection.h
+++ b/src/server/e-ews-connection.h
@@ -134,7 +134,9 @@ typedef void (*EEwsAutoDiscoverCallback) (EwsUrls *urls, gpointer user_data, GEr
 void		e_ews_autodiscover_ws_url	(EEwsAutoDiscoverCallback cb,
 						 gpointer cbdata,
 						 const gchar *email,
-						 const gchar *password);
+						 const gchar *password,
+						 const gchar *ews_url,
+						 const gchar *username);
 void		e_ews_connection_set_mailbox	(EEwsConnection *cnc,
 						 const gchar *email);
 /* Sync folder items */
diff --git a/src/server/tests/test-connection.c b/src/server/tests/test-connection.c
index 5d0a45c..b8afd2e 100644
--- a/src/server/tests/test-connection.c
+++ b/src/server/tests/test-connection.c
@@ -120,24 +120,24 @@ con_test_autodiscover()
 	user_data->test_case = g_strdup ("postive case... \n");
 	g_print ("Testing %s \n", user_data->test_case);
 	user_data->positive_case = TRUE;
-	e_ews_autodiscover_ws_url (autodiscover_cb, user_data, email, password);
+	e_ews_autodiscover_ws_url (autodiscover_cb, user_data, email, password, NULL, NULL);
 
 	user_data = g_new0 (struct _cb_data, 1);
 	user_data->test_case =	g_strdup ("wrong password... \n");
 	/* It does respond properly with the url, Check it out */
 	user_data->positive_case = TRUE;
 	g_print ("Testing %s \n", user_data->test_case);
-	e_ews_autodiscover_ws_url (autodiscover_cb, user_data, email, "wrongpassword");
+	e_ews_autodiscover_ws_url (autodiscover_cb, user_data, email, "wrongpassword", NULL, NULL);
 
 	user_data = g_new0 (struct _cb_data, 1);
 	user_data->test_case = g_strdup ("email without domain ... \n");
 	g_print ("Testing %s \n", user_data->test_case);
-	e_ews_autodiscover_ws_url (autodiscover_cb, user_data, "wronguseremail", password);
+	e_ews_autodiscover_ws_url (autodiscover_cb, user_data, "wronguseremail", password, NULL, NULL);
 
 	user_data = g_new0 (struct _cb_data, 1);
 	user_data->test_case = g_strdup ("wrong email address and password... \n");
 	g_print ("Testing %s \n", user_data->test_case);
-	e_ews_autodiscover_ws_url (autodiscover_cb, user_data, "godknows@donknow.com", "wrongpassword");
+	e_ews_autodiscover_ws_url (autodiscover_cb, user_data, "godknows@donknow.com", "wrongpassword", NULL, NULL);
 
 	user_data->test_case = g_strdup ("wrong user name ... \n");
 	g_print ("Testing %s \n", user_data->test_case);
@@ -147,7 +147,7 @@ con_test_autodiscover()
 	user_data->test_case = g_strdup ("wrong user name ... \n");
 	g_print ("Testing %s \n", user_data->test_case);
 	user_data->quit = TRUE;
-	e_ews_autodiscover_ws_url (autodiscover_cb, user_data, wrong_username, password);
+	e_ews_autodiscover_ws_url (autodiscover_cb, user_data, wrong_username, password, NULL, NULL);
 	g_free (wrong_username);
 }
 
