1

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.

Limi Derjaj
  • 45
  • 1
  • 10
  • Does this answer your question? [Send daily notification mail from asp.net mvc web app](https://stackoverflow.com/questions/59358040/send-daily-notification-mail-from-asp-net-mvc-web-app) – Lutti Coelho Dec 25 '19 at 14:46

1 Answers1

2

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("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);
            }  
        }  
    }  

}  

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();
    }


Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • i have already added in the global.asax but i have two errors: public class Jobclass:IJob there i have an erro in the IJob ( error is thet i don't return nothing) – Limi Derjaj Dec 19 '19 at 11:52
  • and the other error is in the job scheduler here: IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler(); error is: "Cannot implicity convert type System.Threading.Task to Quartz.IScheduler – Limi Derjaj Dec 19 '19 at 11:54
  • For the first one, change `public Task SendEmail(IJobExecutionContext context)` to `public void SendEmail(IJobExecutionContext context)` – Athanasios Kataras Dec 19 '19 at 11:55
  • For the second one, try `IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler().GetAwaiter().GetResult();` – Athanasios Kataras Dec 19 '19 at 11:58
  • First of all i forgot to say that i am using quartz.net 3.0, however for the first problem i've resolved it adding public class SendMailJob : IJob { public Task Execute(IJobExecutionContext context) { return Task.Factory.StartNew(() => SendMail()); } public void SendMail() { do things } For tje second problem the solution that you give me don't give me errors in code but is it correct? – Limi Derjaj Dec 19 '19 at 14:52