locked
Quartz.net does not send email through WebMail object? why? RRS feed

  • Question

  • User-1651604128 posted

    Hi , I am using Quartz.net in my mvc web app, I set it run every 5 mins and test it in my local host, it works fine except the WebMail does not fire to send email.

    The exact same codes, it works well if I ran the codes manually. but if I set it scheduled by using Quartz.net, the WebMail does not work anymore, no email sent out.

    the codes is like this:

    WebMail.Send(to: "jsmith@pubsxxxxice.com",
                    subject: "emailSubject",
                    body: "this is body"
               
                    );

    And in Global.asax.cs file, I have QuartzScheduler.Start() under Application_Start() method; to fire the quartz.net object. and since I added this line of code, I got "Object reference not set to an instance of an object." when code execute on the line WebMail.Send(to: "jsmith@pubxxxxice.com",

    But if I commented the line of code QuartzScheduler.Start() in Global.asax.cs file, the email is working well.

    Any idea?

    when the c# code running in schedule, the WebMail

    Tuesday, March 3, 2015 3:10 PM

All replies

  • User-1461986491 posted

    Here is the complete codeI have implement in my projects using Quartz.net

    Global.asax,cs

    protected void Application_Start()
    {
         JobScheduler.Start();    
    }

    Here is the Start method impleted in JobScheduler.cs class file

    JobScheduler.cs

    public class JobScheduler
        {
            public static void Start()
            {
    
                IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
                scheduler.Start();
    
                IJobDetail job = JobBuilder.Create<News>().Build();
    
                ITrigger trigger = TriggerBuilder.Create()
                    .WithDailyTimeIntervalSchedule
                      (s =>
                         s.WithIntervalInHours(24)
                        .OnEveryDay()
                        .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0))
                      )
                    .Build();
    
                scheduler.ScheduleJob(job, trigger);
            }
        }

    Then here is the News class implemented.

    In Execute method, I have send the mail...Its works for me.

    public class News :IJob
    {
            public News()
            {
            }
    
            //Sending Mail
            public void Execute(IJobExecutionContext context)
            {
                //Paste your Email function or any code
            }
    }

    Wednesday, March 4, 2015 12:30 AM
  • User-1651604128 posted

    Here is the complete codeI have implement in my projects using Quartz.net

    Global.asax,cs

    protected void Application_Start()
    {
         JobScheduler.Start();    
    }

    Here is the Start method impleted in JobScheduler.cs class file

    JobScheduler.cs

    public class JobScheduler
        {
            public static void Start()
            {
    
                IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
                scheduler.Start();
    
                IJobDetail job = JobBuilder.Create<News>().Build();
    
                ITrigger trigger = TriggerBuilder.Create()
                    .WithDailyTimeIntervalSchedule
                      (s =>
                         s.WithIntervalInHours(24)
                        .OnEveryDay()
                        .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(0, 0))
                      )
                    .Build();
    
                scheduler.ScheduleJob(job, trigger);
            }
        }

    Then here is the News class implemented.

    In Execute method, I have send the mail...Its works for me.

    public class News :IJob
    {
            public News()
            {
            }
    
            //Sending Mail
            public void Execute(IJobExecutionContext context)
            {
                //Paste your Email function or any code
            }
    }

    Thanks a lot, my scheduler setup is similar as your, as I mentioned in my email, it works as my schedule for every 5 mins, in the job, I have methods to save records to SQL table, and send email after the document is saved, the document is saved, but the email can not be saved. The WebMail object return null, that is why the email can not sent. So to be cleared, I want to know why the WebMail object is null when it is in scheduler, and the Webmail is working fine if I run it manually. By the way, all of the testing is done in local machine.

    Any idea?

    Wednesday, March 4, 2015 8:27 AM