Last active
November 22, 2018 03:25
-
-
Save mattbenic/400e3c039ab8ea3e33aa to your computer and use it in GitHub Desktop.
Extension method to have SmtpClient's SendMailAsync respond to a CancellationToken
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
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("[email protected]", 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.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@cwellsx: Looking at the source,
SendMailAsync
just wrapsSendAsync
, so callingSendAsyncCancel
will work.