fearofawhackplanet
fearofawhackplanet

Reputation: 53446

Async email from .Net

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.

Upvotes: 2

Views: 1848

Answers (3)

Bronumski
Bronumski

Reputation: 14272

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

Upvotes: 0

MetalMikester
MetalMikester

Reputation: 1077

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.

Upvotes: 0

Tony Abrams
Tony Abrams

Reputation: 4673

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.

Upvotes: 2

Related Questions