views:

42

answers:

3

I'm trying to send an email async so it doesn't slow down my front end (Asp.Net MVC).

SmtpClient smtp = new SmtpClient(_mailServer, 25);
smtp.UseDefaultCredentials = true;
MailMessage message = new MailMessage();

// ...etc

smtp.SendA(message); // this works fine

smtp.SendAsync(message, null); // if i change it to this, it doesn't work (mail never appears)

I don't really undestand what the 2nd param to SendAsync is for.

MSDN says its an object to pass to the method that is invoked when the operation completes

well, wtf? what method? So I've just tried passing null as I don't really understand what this is for, but obviously something is wrong.

+1  A: 

Essentially it's an object that you want passed in the send completed event.

When you use SendAsync, the event SendCompleted occurs. You then handle that event so that you know that you can send another email. The main reason for this is because you can only send one email at a time.

Tony Abrams
A: 

Do you have an event handler set for the SendCompleted event? In the MSDN sample for SmtpClient.SendAsync, userState is just a string, but they have a callback function assigned to the SendCompleted event. That might just be what's missing here.

MetalMikester
A: 

More than likely your application is ending before the email has been sent. The second parameter is passed to the on complete event handler.

Have a look at the example from MSDN and try that in isolation.

http://msdn.microsoft.com/en-us/library/x5x13z6h.aspx

Bronumski