Exchange Web Services Autodiscover fails after first login failure

Hi,

I am writing a C#.NET to send emails using Exchange Web Services APIs. I could find that the code works perfectly when I give the correct user name and password. But, if I give a wrong password on first attempt, the Autodiscover seldom Succeeds.

Here is my sample code:

try
{
	m_EWService = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
	m_networkCredential = new NetworkCredential();

	GetCredentials(sMailbox, out m_networkCredential);

	m_EWService.Credentials = m_networkCredential;
	m_EWService.AutodiscoverUrl(m_networkCredential.UserName, RedirectionCallback);

}
catch (Exception ex)
{
	System.Console.WriteLine(ex.Message);
	ShowMessage(ex.HResult, ex.Message);
}

public static void GetCredentials(string serverName, out NetworkCredential networkCredential) { CREDUI_INFO credui = new CREDUI_INFO(); credui.pszCaptionText = "Please enter the credentails for " + serverName; credui.pszMessageText = ""; credui.cbSize = Marshal.SizeOf(credui); uint authPackage = 0; IntPtr outCredBuffer = new IntPtr(); uint outCredSize; bool save = true; int result = CredUIPromptForWindowsCredentials(ref credui, 0, ref authPackage, IntPtr.Zero, 0, out outCredBuffer, out outCredSize, ref save, 0x1 /* Generic */); var usernameBuf = new StringBuilder(100); var passwordBuf = new StringBuilder(100); var domainBuf = new StringBuilder(100); int maxUserName = 100; int maxDomain = 100; int maxPassword = 100; if (result == 0) { if (CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, usernameBuf, ref maxUserName, domainBuf, ref maxDomain, passwordBuf, ref maxPassword)) { //clear the memory allocated by CredUIPromptForWindowsCredentials CoTaskMemFree(outCredBuffer); networkCredential = new NetworkCredential() { UserName = usernameBuf.ToString(), Password = passwordBuf.ToString(), Domain = domainBuf.ToString() }; return; } } networkCredential = null; }

Can someone throw some light into how to make it working after the login failure;

  • Moved by Kristin Xie Friday, March 13, 2015 5:58 AM move to appropriate forum
March 12th, 2015 3:01pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics