views:

351

answers:

5

in my web-application I want to send mail for users according to pre selected periods by themselves.
for example: 1 HTML Mail Per 3days to user 01 and 1 HTML Mail Per 20days to user 02

how can I perform that? any resources? I can send mail by my app from My Gmail Account to any mail addresses in my tables but i dont know how to send it automatically in a period of time.(i use C# and SQL Express)
Help me out!

+2  A: 

You will need something which can periodically run jobs for you, like a cron daemon or windows task scheduler.

Essentially you have the periodic job kick off and do whatever mail handling you need.

You can also do this from code if you can create a windows service to basically sleep until the next batch of mails needs to be sent.

The easiest is to write the task scheduler or cron job to run periodically. In that way you just need a small piece of code to handle the mail sending portion and then you just schedule it to run once an hour or day or whatever needed.

GrayWizardx
+1  A: 

i think there a solution:

1- you have to add 2 column in the user table in your sql db if you have a user table and in the first column add the date of the last email sent to the user and the second column has the period for sending the email for that user for ex: LastEmailSentDate datetime SendEmailPeriod int

2- in your application code write a function that compare the last date of the last sent email with the period of the sending the email.

// here the funciton code

public void CompareLastSentDate()
{
   // lets assume that you bring the data for the db using Sqdatareader reader
   //get the field from the LastEmailSentDate field in the  database as i mention before
   DateTime LastEmailSentDate = Convert.ToDate(reader["DatePeriod"]) 

   // get the field from the SendEmailPeriod of the user field from database
   int sendEmailPeriod =  Convert.Toint32(reader["SendEmailPeriod"]) 

   // now you have the date before the period of day ex: before 3 days depend on user
   DateTime DatePeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, (DateTime.Now.Day - sendEmailPeriod ));

   // if the last email send is before period of day that mean u have to send an email again
   if(LastEmailSentDate.Day <= DatePeriod.Day)
   {
      // sent the email to the user
   }

}

note: now u can loop among the users and sent the email

you can call this function once in a day by calling it from ur app home page Page_Load event and after the first call of the day add an application["LastFunctionCallDate"] = DateTime.Now flag so in the next you can check this flag if its == today and if not call it again

peacmaker
but we should call CompareLastSentDate() function periodically. you know this just send mail for users(if I loop your suggested method among users) once and what about the next period?? in fact this function should call every day and check whether send mail or not today. but thank your answer.
mahdiahmadirad
check my answer now i think u will find solution to this too
peacmaker
+1  A: 

The great thing about IIS hosted ASP.NET is that IIS will (by default) periodically recycle your application pool according to the settings on the app pool itself.

When your application pool is starting (which could be at least once a day especially if it's allowed to idle i.e. a business app where most activity is 9-5) the Application_Start event-handler in Global.asax is fired. This could be used for your recurring task.

Now you don't necessarily want to run this email send synchronously within that Application_Start handler because to me it seems this messaging functionality is not core to the startup but by all means use this event-handler as an easy way to periodically do your housekeeping.

To send async you should use async delegates for example.

Famous Nerd
+1  A: 

I found the Solution.
according to my search we have 3 ways to handle that:

  1. working with SQL Server to send mail notification in periods of time.(or this)
  2. using Windows service and Creating Timer object and checking the time with it.

but in ways 1 and 2 we should access to server and we need dedicated hosting server to for example installing WinService on it. so it does not work in a sharing Host space we usually use. So I Found the best way as you see:

  3.  Simulating Windows Services Using ASP.NET Caching For Scheduled Jobs.

the link above is a terrific solution. So there is no need to work out-side of our web application.

mahdiahmadirad
+2  A: 

Hai,

Have a look at quartz.net

Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems.

Quartz.NET is a port of very propular open source Java job scheduling framework, Quartz. Quartz.NET supports clustering and database persistence out-of-the-box and has powerful means to schedule jobs using cron like expressions, interval triggers and exclusion advices.

Pandiya Chendur