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 1 hour 21 minutes ago move to appropriate forum
March 12th, 2015 11:03am

Hi Santhosh,

Thanks for posting in MSDN forum.

Based on your description, your case related to EWS(Exchange Web Services) APIs.

so I would move it to Exchange Development forum to see whether there is an solution.

Best regards,

Kristin

Free Windows Admin Tool Kit Click here and download it now
March 13th, 2015 1:59am

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

Other recent topics Other recent topics