Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Synchronous SmtpClient send, doesn't hit SendCompleted handler

Tags:

c#

 SmtpClient client = new SmtpClient("my.server.com");

I am creating the client as usual and send an email. All works fine, the email arrives in the box correctly.

Still i want to do some work in code inside the SendCompleted event handler.

  client.SendCompleted += client_SendCompleted;

I'm starting the work without the async option,

client.Send(message);

but this way, my handler code is never hit.

If i choose client.SendAsync(), the event handler gets executed but i have to do some synchronizations to get it right and maybe there is an easier way for this..

So my question , when i use send without async, is it normal to not hit the handler?

like image 265
Olaru Mircea Avatar asked Sep 20 '25 09:09

Olaru Mircea


1 Answers

If you are sending synchronously then your completed handler will never fire because the associated event is only triggered for async calls.

In a synchronous call, just put whatever code you would have put in your handler after your call to Send:

client.Send(message);
//TODO: Put your handler code here.
like image 71
Brian Driscoll Avatar answered Sep 22 '25 22:09

Brian Driscoll