How to send email daily with Quartz.net

后端 未结 2 967
花落未央
花落未央 2021-01-07 10:48

I\'m try to use Quartz.net send email at time on everyday in asp.net MVC project. But it\'s work only once, it\'s not repeat everyday. Here my code

public cl         


        
相关标签:
2条回答
  • 2021-01-07 11:20

    Avoid .RepeatForever(), instead use .WithDailyTimeIntervalSchedule with .StartingDailyAt

    Example:

    IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
    scheduler.Start();
    var hour = 12 //Start Time
    var minute=15 //Start minute 
    
    IJobDetail job = JobBuilder.Create<Job_SendMail>().Build();
    var time = DateTime.Now.AddSeconds(10);
    ITrigger trigger = TriggerBuilder.Create()
        .WithDailyTimeIntervalSchedule
          (s =>
             s.WithIntervalInHours(24)
            .OnEveryDay()
            .StartingDailyAt(TimeOfDay.HourMinuteAndSecondOfDay(hour, minute, time.Second))
          )
        .Build();
    
    scheduler.ScheduleJob(job, trigger);
    
    0 讨论(0)
  • 2021-01-07 11:23

    I think you are missing .RepeatForever(). Your schedule will only run once. But if it is very important that the mails are send at 8 sharp, then you should be using a server side solution like a service or scheduled task because there is no guarantee that the website is available.

    You can use a job that runs every x minutes while the site is online. And should the apppool recycle, websites crashes or whatever, when it's online again it will immediately send the mails and again do so every 10 minutes.

        IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
        scheduler.Start();
    
        IJobDetail job1 = JobBuilder.Create<Job_SendMail>().Build();
        ITrigger trigger1 = TriggerBuilder.Create()
            .WithIdentity("trigger1", "group1")
            .StartNow()
            .WithSimpleSchedule(x => x
                .WithIntervalInMinutes(10)
                .RepeatForever())
            .Build();
        scheduler.ScheduleJob(job1, trigger1);
    

        public class Job_SendMail: IJob
        {
            void IJob.Execute(IJobExecutionContext context)
            {
                //do stuff
            }
        }
    
    0 讨论(0)
提交回复
热议问题