-
-
Save mattbenic/400e3c039ab8ea3e33aa to your computer and use it in GitHub Desktop.
| 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); | |
| } | |
| } | |
| } |
Thanks, that's a good suggestion
Does SendMailAsync cancel when SendAsyncCancel is called?
MSDN says "Use the SendAsyncCancel method to cancel a pending SendAsync operation" so I fear that SendAsyncCancel affects SendAsync but not SendMailAsync.
@cwellsx: Looking at the source, SendMailAsync just wraps SendAsync, so calling SendAsyncCancel will work.
Also for what it's worth SmtpClient is IDisposable and Dispose seems to tidy up any in-flight requests. So if you can instantiate your own SmtpClient (rather than taking an arbitrary one in to your extension method) you can avoid the race condition of calling Register(...SendAsyncCancel) with something like:
using (var smtpClient = new SmtpClient())
using (cancellationToken.Register(smtpClient.Dispose))
await smtpClient.SendMailAsync("xxx@yyy.com", emailAddress, subject, body);
Although now you've got this weird double-dispose thing going on. (Which is harmless, but the method of calling SendAsyncCancel conveys the intent of what you're doing more clearly, in my opinion.)
I don't think this will work if the
CancellationTokenis already cancelled.CancellationToken.Registerimmediately and synchronously runsRegistercallbacks when theCancellationTokenis already cancelled."If this token is already in the canceled state, the delegate will be run immediately and synchronously"
So
...Register(cancelSend)will immediately callclient.SendAsyncCancelbeforeSendMailAsync.SendAsyncCancel is a no-op if no messages are in-flight.
I think you want something like: