Limi Derjaj
Limi Derjaj

Reputation: 45

Send daily mail in background from asp.net MVC web app using Quartz.net

i have developed an asp.net MVC web app, and now i want to send a daily e-mail in background. I want to add this functionality to an external project which I have already created. In that project, I have created this class, i need some help how to made this work.

public class SendMailJob : IJob
{
public Task SendEmail(IJobExecutionContext context)
{

    MailMessage Msg = new MailMessage();

    Msg.From = new MailAddress("[email protected]", "Me");

    Msg.To.Add(new MailAddress("[email protected]", "ABC"));

    Msg.Subject = "Inviare Mail con C#";

    Msg.Body = "Mail Sended successfuly";
    Msg.IsBodyHtml = true;

    SmtpClient Smtp = new SmtpClient("smtp.live.com", 25);

    Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

    Smtp.UseDefaultCredentials = false;
    NetworkCredential Credential = new
    NetworkCredential("[email protected]", "password");
    Smtp.Credentials = Credential;

    Smtp.EnableSsl = true;

    Smtp.Send(Msg);

//CONFIGURE JOB TO EXECUTE DAILY
// define the job and tie it to our SendMailJob class
IJobDetail job = JobBuilder.Create<SendMailJob>()
    .WithIdentity("job1", "group1")
    .Build();

// Trigger the job to run now, and then repeat every 24 hours
ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("trigger1", "group1")
    .StartNow()
    .WithSimpleSchedule(x => x
        .WithIntervalInHours(24)
        .RepeatForever())
    .Build();
}
}

I didn't understand how Quartz.net works very well, the problems I encountered are the following:

The code to send the mail work well, i have tried it putting a button in the index view and when pressed it invokes an action from the controller that use the code i wrote.

Upvotes: 1

Views: 1859

Answers (1)

Athanasios Kataras
Athanasios Kataras

Reputation: 26450

Check this guide here

Steps you need to take:

  1. You need to create a job scheduler that will do the scheduling for you
    using System;  
    using System.Collections.Generic;  
    using System.Linq;  
    using System.Web;  
    using Quartz;  
    using Quartz.Impl;  

    namespace ScheduledTask.Models  
    {  
        public class JobScheduler  
        {  
            public static void Start()  
            {  
                IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();  
                scheduler.Start();  

                IJobDetail job = JobBuilder.Create<SendMailJob>().Build();  

                ITrigger trigger = TriggerBuilder.Create()  
                .WithIdentity("trigger1", "group1")  
                .StartNow()  
                .WithSimpleSchedule(x => x  
                .withIntervalInHours(24)  
                .RepeatForever())  
                .Build();  

                scheduler.ScheduleJob(job, trigger);  
            }  
        }  
    }  
  1. Recreate your sending function to something like this
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using Quartz;  
using System.Net;  
using System.Net.Mail;  


namespace ScheduledTask.Models  
{  
    public class Jobclass:IJob  
    {       
        public void Execute(IJobExecutionContext context)  
        {  
                MailMessage Msg = new MailMessage();

                Msg.From = new MailAddress("[email protected]", "Me");

                Msg.To.Add(new MailAddress("[email protected]", "ABC"));

                Msg.Subject = "Inviare Mail con C#";

                Msg.Body = "Mail Sended successfuly";
                Msg.IsBodyHtml = true;

                SmtpClient Smtp = new SmtpClient("smtp.live.com", 25);

                Smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

                Smtp.UseDefaultCredentials = false;
                NetworkCredential Credential = new
                NetworkCredential("[email protected]", "password");
                Smtp.Credentials = Credential;

                Smtp.EnableSsl = true;

                Smtp.Send(Msg);
            }  
        }  
    }  

}  

In global.asax.cs in the application start event

    protected void Application_Start(Object sender, EventArgs e)
    {
        // keep whatever other code is there
        JobScheduler.Start();
    }


Upvotes: 2

Related Questions