Use the AsyncCompletedEventArgs to capture errors in Async StmpClient SendAsync calls.

By [)ia6l0 iii

In the SMTPClient class of .net, we use the SendAsync method to send emails asychronously. But we fail to retrieve the errros in such async calls.

We can use the AsyncCompletedEventArgs in the SendCompleted event of the SMTPClient to retrieve the errors.

Sample code:
SmtpClient client = new SmtpClient(_smtpServer);
client.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);
client.SendAsync(message, ""Sending.."");

private static void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
            if (e.Error != null)
            {
                 //here is the error.
             }
}

Use the AsyncCompletedEventArgs to capture errors in Async StmpClient SendAsync calls.  (1910 Views)