How to send Email without delaying the response in ASP.NET?

I have tried a lot of variations to return the response quickly to user and send email in background but failed. Here is one sample that I have tried,

ThreadPool.QueueUserWorkItem(callback => 
                // It is important to put this line inside try catch otherwise it might crash the process 
                client.SendAsync(message, message)
                );

When I put a break point on client.SendAsync, no respose will be sent to user until this line execute. So what is the proper way to send email in background without waiting for response.

Update: I think I got the issue, actually it's the VS debugger that stop the asp.net thread(suspend ) until you Step Through. The above code works for my scenario.

Upvotes: 3

Views: 259

Answers (3)

Joe
Joe

Reputation: 1669

Send Email Asynchronously with ASP.NET http://www.asp.net/web-forms/videos/how-do-i/how-do-i-send-email-asynchronously-with-aspnet

Upvotes: 1

DanP
DanP

Reputation: 6478

Depending upon the number and size of the emails, you might also consider writing them to a pickup folder as well. You can use a local IIS SMTP server to relay the mail through a smart host. In practice, this has worked very well for us.

Upvotes: 1

My Other Me
My Other Me

Reputation: 5117

You could place the email in a Message Queue and process the actual sending of it in a separate service that reads from that Message Queue.

Upvotes: 1

Related Questions