tags:

views:

272

answers:

5

Hi all, what is the best way to send an email from an asp.net page?

I don't mean the code to actually send the message, I mean, for example, in my application, the lost password link sends an email to the user when a button is clicked, but if the smtp server takes 10 seconds to send the message, the user will wait the page to load for 10 seconds.

What would you suggest to avoid this?

thanks!

A: 

I've never done this, but off the top of my head, I'd suggest developing a background process that's separate from your website. This process scans a database table for email addresses; whenever it finds a new one, it sends a confirmation email to that address and then deletes it from the list.

Meanwhile, whenever a user submits your ASP.NET form, their email is added to the same database table.

Thus, you have de-synchronized the email sending.

Some general info about creating background processes:

http://www.codeproject.com/KB/cs/tsnewlib.aspx

http://forums.asp.net/t/1134253.aspx

jonathanconway
A: 

Invoke the email sending via an Ajax call to a web service. Alternatively you could use the asynchronous send method on the SmtpClient class

ckramer
The latter option looks like a great solution. I wouldn't go with Ajax, since it relies on the client having Javascript enabled.
jonathanconway
There are people who don't have javascript enabled? :)
ckramer
Not any who can read this comment :P
tarn
Lol most browsers have it on, but as a general rule, you should try not to make basic functionality rely on javascript. Some mobile phones and other devices don't support JS.
jonathanconway
A: 

It is very common to process email and other long running processes in a separate thread or process. This is pretty easy to setup with a messaging queue system or a regular background batch type process.

Having said that, I think you should also consider the async methods noted by ckramer.

Another option is to have a local SMTP server.

tarn
+1  A: 

asynch send will not work on an asp.net worker thread (try it - it throws an exception), so either spawn a secondary thread, send the email in a popup window, or just let the user wait.

and does it really take 10 seconds? i've never seen it take more than 1...

Steven A. Lowe
A: 

Create a database for all outgoing mails and process it in another thread.

Spikolynn