1

I have complete code for send daily mail to expiration mail to user. my description of code is below:

Global.asax :

void Application_Start(object sender, EventArgs e) 
{

    System.Timers.Timer myTimer = new System.Timers.Timer();        
    double inter = (double)GetNextInterval();        
    myTimer.Interval = inter;        
    myTimer.AutoReset = true;
    myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
    myTimer.Enabled = true; 
}

private double GetNextInterval(){

    string timeString;
    timeString = "12:00 AM";
    DateTime t = DateTime.Parse(timeString);
    TimeSpan ts = new TimeSpan();
    int x;
    ts = t - System.DateTime.Now;
    if (ts.TotalMilliseconds < 0)
    {
        ts = t.AddDays(1) - System.DateTime.Now;
    }
    return ts.TotalMilliseconds;
}

public void myTimer_Elapsed(object source, System.Timers.ElapsedEventArgs e) {

// My Mail Code }

This code is work for first time mail but i want to daily basis send mail.

How it possible?

Rajeesh Menoth
  • 1,704
  • 3
  • 17
  • 33
Ketul Soni
  • 15
  • 6
  • You can use Windows Service.. – Rajeesh Menoth Dec 14 '15 at 06:08
  • no i use only this code – Ketul Soni Dec 14 '15 at 06:09
  • your GetNextInterval() method returns interval based on application start time – Darshan Faldu Dec 14 '15 at 06:11
  • my suggestion is create windows service or console application and run it in server. – Darshan Faldu Dec 14 '15 at 06:13
  • One challenge of running timers on an IIS process is that the IIS process must be constantly poked. On a very active site, this is not a problem. Constant web requests will keep your process alive. If this is a corporate intranet site, when midnight rolls around, there may not have been any activity for hours and your IIS process has long since shut down. This can be easily remedied by employing some artificial strategy to keep your IIS process alive. – S. Brentson Dec 14 '15 at 07:05
  • One other issue I see with your code is that, while time timer is set to auto reset, the interval won't be predictable. If you must insist on using this approach, you will certainly need to manually reset the timer after each interval. – S. Brentson Dec 14 '15 at 07:07

3 Answers3

2

Create a console application that sends an E-Mail and schedule a task to run your console application every day using task scheduler.

Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • 1
    I think you'll have to add some more details on why you recommend a console app, because I think the OP doesn't know why an IIS-hosted web application doesn't come in handy in this case. – Jan Köhler Dec 14 '15 at 06:21
  • ok 1. It's simple to accomplish your task. 2. the code on your web application gets executed only when the user goes trough the application. – Buda Gavril Dec 14 '15 at 06:28
  • @BudaGavril is right, its the best way to achieve your task is creating a Console Application, it could be installed in the same machine that has your application. For create this service i recommend you to use TopShelf – Pedro Benevides Dec 14 '15 at 08:34
  • @PedroBenevides if a service is created, it starts automatically and it consumes ram and cpu (even if the amount of resources needed to run the service is not big), but the task scheduler service is running and the resources used to send the emails are used only once when the console app starts – Buda Gavril Dec 14 '15 at 08:47
0

You can create Scheduled Tasks for .aspx Files also. It will open a browser and send mail , than u have to create another Scheduled Tasks to close it.

Here is Example :

https://mohammednv.wordpress.com/2008/01/08/how-to-create-scheduled-tasks-for-aspx-files/

Karthikeyan
  • 333
  • 5
  • 17
0

You can schedule any procedure on the code side using Quartz.NET

But there is a problem. If an asp.net application stays inactive ( Not receiving any requests) it is put to sleep from IIS.

So you have 2 options. Change IIS Settings to keep your app pool always awake or poke your application with dummy requests to any method to prevent it from sleeping

My suggestion and implementation is the following which i think is the most professional way of doing it.

Expose your timely tasks as wcf services.

Develop a Windows Service which runs Quartz and invokes those wcf services in a timely manner.

The pros are you get to keep your business logic code in one place and it works also in server farm scenarios

Community
  • 1
  • 1
Anestis Kivranoglou
  • 7,728
  • 5
  • 44
  • 47