Note..NET 4.5 SmtpClient implements the async awaitable SendMailAsync . For lower versions, use SendAsync as described below.
You should always IDisposable instances of IDisposable as soon as possible. In the case of asynchronous calls, this occurs in the callback after sending the message.
var message = new MailMessage("from", "to", "subject", "body")) var client = new SmtpClient("host"); client.SendCompleted += (s, e) => { client.Dispose(); message.Dispose(); }; client.SendAsync(message, null);
A bit annoying SendAsync does not accept the callback.
TheCodeKing Sep 01 2018-11-21T00: 00Z
source share