SmtpClient.SendAsync code review
Posted
by Lieven Cardoen
on Stack Overflow
See other posts from Stack Overflow
or by Lieven Cardoen
Published on 2010-04-19T12:28:24Z
Indexed on
2010/04/19
12:33 UTC
Read the original article
Hit count: 837
I don't know if this is the way it should be done:
{
...
var client = new SmtpClient {Host = _smtpServer};
client.SendCompleted += SendCompletedCallback;
var userState = mailMessage;
client.SendAsync(mailMessage, userState);
...
}
private static void SendCompletedCallback(object sender,
AsyncCompletedEventArgs e)
{
// Get the unique identifier for this asynchronous operation.
var mailMessage= (MailMessage)e.UserState;
if (e.Cancelled)
{
Log.Info(String.Format("[{0}] Send canceled.", mailMessage));
}
if (e.Error != null)
{
Log.Error(String.Format("[{0}] {1}", mailMessage, e.Error));
}
else
{
Log.Info("Message sent.");
}
mailMessage.Dispose();
}
Disposing the mailMessage after the client.SendAsync(...) throws an error. So I need to dispose it in the Callback handler.
© Stack Overflow or respective owner