Reputation: 45
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
Reputation: 26450
Check this guide here
Steps you need to take:
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);
}
}
}
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