Skip to content

Instantly share code, notes, and snippets.

@njmube
Forked from mattbenic/SmtpClientExtensions.cs
Created December 1, 2015 05:33
Show Gist options
  • Save njmube/2990f638f33204c0bae2 to your computer and use it in GitHub Desktop.
Save njmube/2990f638f33204c0bae2 to your computer and use it in GitHub Desktop.
Extension method to have SmtpClient's SendMailAsync respond to a CancellationToken
using System;
using System.Net.Mail;
using System.Threading;
using System.Threading.Tasks;
public static class SmtpClientExtensions
{
/// <summary>
/// Extension method to have SmtpClient's SendMailAsync respond to a CancellationToken
/// </summary>
public static async Task SendMailAsync(
this SmtpClient client,
MailMessage message,
CancellationToken token)
{
Action cancelSend = () =>
{
client.SendAsyncCancel();
};
using (var reg = token.Register(cancelSend))
{
await client.SendMailAsync(message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment