0

I'm trying to send email automatically on 00:00; I've used Quartz.net but there's problem about medium trusted environment (It's shared hosting). I've read this on stackoverflow but didn't get any idea how to do that.

Another I can't create windows service as it's shared hosting.

Is there any other way to do that?

Community
  • 1
  • 1
gsiradze
  • 4,583
  • 15
  • 64
  • 111
  • Is task scheduler available on your shared hosting? – Guru Stron Sep 25 '15 at 09:24
  • if you can, use a console application and have the task scheduler run it. – user1666620 Sep 25 '15 at 09:25
  • @GuruStron idk it's hostgator personal plan – gsiradze Sep 25 '15 at 09:28
  • @user1666620 I can't. I'm uploading my application there using cpanel – gsiradze Sep 25 '15 at 09:29
  • @George, as i see you have two options here: create application with infinite loop which will do something using recurring [timer](http://stackoverflow.com/questions/18611226/c-how-to-start-a-thread-at-a-specific-time), or an app which will do basically the same but in foreground thread. – Guru Stron Sep 25 '15 at 09:41
  • @GuruStron thanks I'll try that. But. I'm not expert at threading and will it execute for every user or only once? for example if there's online 10 user will it execute 10 times? – gsiradze Sep 25 '15 at 09:46
  • @George, missed that you are uploading asp app, so you just can on app start create timer which will execute `send email` and start new one. remember create something for sync(db table/or just file, which will track that todays emai was sent, and do or do not depending on it=), cause app pool can be restarted – Guru Stron Sep 25 '15 at 09:47
  • @GuruStron can you provide with simply as answer? – gsiradze Sep 25 '15 at 09:50

1 Answers1

1

It's not a very good practise, but you can do something like that(for example i'm using MVC 4):

In Global.asax you can try to do next:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        ...//your current code
        SendEmail();
        SetUpTimer();
    }


    private static System.Threading.Timer _timer;

    private void SetUpTimer()
    {
        TimeSpan timeToGo = DateTime.Now.AddDays(1).Date - DateTime.Now; //timespan for 00:00 tommorrow 

        _timer = new System.Threading.Timer(x => SendEmail(), null, timeToGo, new TimeSpan(1,0,0,0));
    }

    public void SendEmail()
    {
        //check if email was sent today if not send one
    }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • That may or may not work, since it will depend on the site / app pool to be running. Considering idle timeouts, recycle settings etc.. – oɔɯǝɹ Sep 25 '15 at 10:15
  • 1
    yep, it will not guarantee sending email at 00:00 but assuming that OP has only option to upload adp.net app i see no other options. – Guru Stron Sep 25 '15 at 10:26
  • It sends an email and than throws an exception: "An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code Additional information: Number must be either non-negative and less than or equal to Int32.MaxValue or -1." – gsiradze Sep 25 '15 at 10:52
  • @GuruStron timetogo is getting {-14:52:20.8256074} – gsiradze Sep 25 '15 at 10:53