Error: System.Net.Mail.SmtpException: Failure sending mail. ---> System.ArgumentException: The IAsyncResult object was not returned from the corresponding asynchronous method on this class. Parameter name: asyncResult at System.Net.Mime.MimeBasePart.EndSend(IAsyncResult asyncResult) at System.Net.Mail.Message.EndSend(IAsyncResult asyncResult) at System.Net.Mail.SmtpClient.SendMessageCallback(IAsyncResult result) --- End of inner exception stack trace ---
private void DispatchMail(MailMessage message, MessageTrackerObject trackInfo)
{
SmtpClient mailClient = new SmtpClient();
mailClient.Host = ConfigurationManager.AppSettings[(Constants.FAXSETTINGS_SMTPSERVER)];
mailClient.Port = int.Parse(ConfigurationManager.AppSettings[(Constants.FAXSETTINGS_SMTPPORT)]);
//mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
NetworkCredential ntCredential = new NetworkCredential();
if (GetStaticSetting(Constants.APPCONFIG_KEY_MAILWINDOWSAUTH).ToLower() == "true")
{
//mailClient.UseDefaultCredentials = true;
}
else
{
ntCredential.UserName = GetStaticSetting(Constants.APPCONFIG_KEY_MAILUSERID);
ntCredential.Password = GetStaticSetting(Constants.APPCONFIG_KEY_MAILPASSWORD);
mailClient.Credentials = ntCredential;
mailClient.UseDefaultCredentials = false;
}
mailClient.EnableSsl = GetStaticSetting(Constants.APPCONFIG_KEY_MAIL_SSL).ToLower() == "true";
mailClient.SendCompleted += new SendCompletedEventHandler(MailClient_SendCompleted);
mailClient.SendAsync(message, trackInfo);
//mailClient.Send(message);
}
private void MailClient_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
string error = "";
MessageTrackerObject data = (MessageTrackerObject)e.UserState;
string msg = string.Format("File: {0}", data.Info);
try
{
foreach (string serial in data.Serials)
{
if (e.Cancelled)
{
error = e.Error != null ? String.Format(" #Error: {0}", e.Error.ToString()) : "";
string cancelled = string.Format("{0} Send canceled. {1}", msg, error);
SetFaxStatus(serial, FaxStatus.Cancelled, cancelled);
}
else if (e.Error != null)
{
error = String.Format("{0} #Error: {1}", msg, e.Error.ToString());
SetFaxStatus(serial, FaxStatus.Error, error);
}
else
{
SetFaxStatus(serial, FaxStatus.Sent, string.Format("{0} Mail sent successfully.", msg));
}
}
//release resource
data.Message.Dispose();
}
catch (Exception ex)
{
}
}
How can i avoid this error?