AYK
AYK

Reputation: 3322

Delay send emails using MailMessage and SmtpClient or MessageQueue

I am using SmtpClient and MailMessage to send emails from ASP.Net code. I want to be able to delay the delivery of emails based on business logic. I will be able to generate a DateTime object about when the email should actually be delivered.

I am aware that I can create a Windows Service to schedule email delivery using Sql Server database as an email storage. But I do not want to deploy an extra service on the hosting server.

I am also aware that I can create a SQL Server job to achieve this, but I would not like to, until and unless, this cannot be achieved using pure ASP.NET

A point to note is that, my code checks whether MSMQ is enabled on the server. If it is, MessageQueue class is used for email delivery. So any pointers on achieving this using queues will be helpful.

By the way, the solution should work in both cases (i.e. with SmtpClient as well as MessageQueue.

I have already reviewed this and this questions and its accepted answers. I do not want to use 3rd party products until and unless this cannot be achieved using pure ASP.NET

P.S. .Net Framework version : 3.5, IIS 7 and Sql Server 2008 should be the environment.

Upvotes: 2

Views: 3143

Answers (2)

AYK
AYK

Reputation: 3322

After some research and clarifications from client, it was decided that I should create a long running background thread, which will handle sending of emails at pre-defined times and need not bother about whether the application pool has been shut down due to inactivity.

Upvotes: 0

CodingBarfield
CodingBarfield

Reputation: 3398

Some information about keeping a timer running in asp.net including a great comment about an always on scenario:

Start timer on web application start

You can use the following to keep asp.net from shutting down:

Can you prevent your ASP.NET application from shutting down?

You can simply use the timer to check a database and send all pending mail.

Upvotes: 1

Related Questions