views:

47

answers:

4

I have a data entry and editing form and in every data entry or update event, I have to send an email to a dynamic list of recipients. I have been sending the email as soon as the user clicks the save or edit buttons but am thinking of first saving the data to the database, and then sending the email later. I want to do this partly to improve the response time of the application as the email sending tends to take a long time than desired.

Has any one done some thing some how related to this, is there a better way of implementing something similar or does one know a good tutorial on such.

The email body is html formatted.

+2  A: 

You could write a Windows Service that handles sending your emails, then use a Message Queue as the method of passing data from your application to the service. I.e. your applicaiton saves the data, then adds a message to the Queue. The service continually polls the queue for messages, sending each one as an email.

ck
Orr you skip message queue (separate install, often not available) and use service broker (same functionality within sql server).
TomTom
A: 

you can use threading concept

Bala
+1  A: 

I agree with ck about using a service and a message queue, but there are some alternatives.

One is to use a service that polls the database at a regular interval. This lets you avoid the message queue at the cost of a higher cpu load (the service will do many unnecessary database calls).

You could also do this directly in the database using either a database trigger or a scheduled job in the database. The latest versions of SQL Server supports running stored procedures written in C# or Vb.Net so you could probably reuse much of your existing code here.

Finally you could go for a simple solution where you do the email sending on a separate thread in your asp.net application. This way you avoid the need of a service application and you can reuse your code more or less as it is today.

Rune Grimstad
Rune Grimstad - i tried sending on a separate thread in my asp.net application but it some how isn't making any difference
Name.IsNullOrEmpty
i think i might seriously consider a database trigger or a scheduled job in the database
Name.IsNullOrEmpty
How did you start the new thread? I think it should work if you use the ThreadPool.QueueUserWorkItem method. Ref: http://haacked.com/archive/2009/01/09/asynchronous-fire-and-forget-with-lambdas.aspx
Rune Grimstad
A: 

One way to do this is write to the database, and then put a message on a queue that tells an email service (written as a Windows service) that there are emails to send. The email service then talks to the database to find what it actually needs to do. This decouples the email service from the web application and also avoids polling.

This is slightly different to ck's solution in that the queue message is used as a trigger rather than containing the email information. This decouples the web app and the email service to some extent, and means the email service can be reused by multiple clients without each client having to observe (and keep in step with) the same email message format.

RoadWarrior