-5

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

  • 5
    Have you tried anything? – Jack Miller Jul 14 '17 at 15:53
  • 3
    [so] is *not* a free code writing service. You are expected to try to **write the code yourself**. After [doing more research](http://meta.stackoverflow.com/questions/261592) if you have a problem you can **post what you've tried** with a **clear explanation of what isn't working** and providing a **[mcve]**. I suggest reading [ask] a good question and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/). Also, be sure to take the [tour]. – Igor Jul 14 '17 at 15:53
  • 2
    Try to use programming to do it – Joe Phillips Jul 14 '17 at 15:55
  • 1
    Here's a novel idea: don't actually send the email until it's ready to be sent. Just store it somewhere until it's ready. – mason Jul 14 '17 at 15:56
  • Ok I am not asking anybody to do my job, just asking if there is anybody that has done something like what i am asking. The reason I posted the question is because looking here: https://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient(v=vs.110).aspx there is no OnSend or beforeSend or something like that. The SmtpClient class only provides SendCompleted event – Oscar Cabrera Rodríguez Jul 14 '17 at 16:02
  • You'll need a way to intercept the message first, How is the message being sent? Can you host a smart relay that does this? do you need to program something new to do it or do any existing tools already perform this functionality (there are lots of smart smtp servers out there)? What exactly is the question again? – Joe_DM Jul 14 '17 at 16:04
  • [What is the XY problem?](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – EJoshuaS - Stand with Ukraine Jul 14 '17 at 16:08
  • @EJoshuaS and Joe_DM i just edited the question. Hope it is clear now – Oscar Cabrera Rodríguez Jul 14 '17 at 16:09
  • Incidentally, I assume that this is an email client you wrote (or otherwise control the code of)? Or are you trying to do an Outlook plugin or something like that? (This is a very different question if you're trying to do this with a third party tool like Outlook). – EJoshuaS - Stand with Ukraine Jul 14 '17 at 16:15
  • Also, do you control the SMTP server(s)? – EJoshuaS - Stand with Ukraine Jul 14 '17 at 16:17
  • I have already done a plugin for outlook that does the same. VERY BIG DIFFERENCE is that Outlook object model DO expose an Item_Send event that allows me to cancel the sending operation. I am trying to generalize the solutions to all mail clients, that is why i ran into this. And no, I do not have any control on the SMTP server – Oscar Cabrera Rodríguez Jul 14 '17 at 16:18

1 Answers1

1

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.