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("mymail@mail.com", "Me");
Msg.To.Add(new MailAddress("receivermail@mail.com", "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("mymail@mail.com", "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:
- I don't know how to make this function work (do I have to call the job on my web app? like in the index view if i want to start the job when i get to the index view? And how?)
- I have an error in public Task SendEmail, it says: "not all code path return a value" but what i have to return?
- Am I missing something to make quartz.net work?
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.