Search code examples
c#smtpclient

C# cancel an email sending using smtpClient


I woulkd like to intercept the send event, and cancel the email before it is actually sent. I meand: every time a user clicks send button in a client (using smpt of course) i want to cancel the send operation and add some custom stuff to email's attachments (if any) and body

EDIT:

Here it goes what i would like to achieve: When a user clicks on send button using, lets say Mail app from Windows 10, I do not want the mail to be actually sent, I just want to cancel the send operation, modify the email message (body, attachments), and only then sendit


Solution

  • Quite bluntly, this has XY problem written all over it.

    Once you call Send, there is no mechanism to cancel the message - the class doesn't raise any events before it sends the email or anything, it just sends it. Just make sure you're actually ready to send the message before you send it.

    The actual use case for this is quite small anyway; suppose, just for argument, that the Send operation takes 2 seconds (and it could easily take a lot less); what are the odds that someone will change their mind (or even be able to act quickly enough to stop it from going out) in those 2 seconds?

    You'd be better off building the email message completely and then sending it (rather than calling Send, canceling, finishing building it, and then sending it again).

    TL;DR You can't, shouldn't, and don't need to cancel the Send operation - just don't start it until you know you're ready.