Skip to content

Instantly share code, notes, and snippets.

@tuan
Last active February 6, 2016 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuan/ac646eb0ef175edfc069 to your computer and use it in GitHub Desktop.
Save tuan/ac646eb0ef175edfc069 to your computer and use it in GitHub Desktop.
SendMailAsync with cancellation token
public class SmtpClientWrapper : IDisposable
{
private SmtpClient _smtpClient;
public SmtpClientWrapper(SmtpClient smtpClient)
{
this._smtpClient = smtpClient;
}
public async Task SendMailAsync(MailMessage message, CancellationToken ct)
{
using (ct.Register(_smtpClient.SendAsyncCancel))
{
await _smtpClient.SendMailAsync(message);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool p)
{
if (_smtpClient != null)
{
_smtpClient.Dispose();
_smtpClient = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment