创建作业以发送电子邮件public class SendMailJob : IJob{ public void Execute(IJobExecutionContext context) { ...Do your stuff; }}然后将您的作业配置为每天执行// define the job and tie it to our SendMailJob classIJobDetail job = JobBuilder.Create<SendMailJob>() .WithIdentity("job1", "group1") .Build();// Trigger the job to run now, and then repeat every 24 hoursITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInHours(24) .RepeatForever()) .Build(); HangFire RecurringJob.AddOrUpdate( () => YourSendMailMethod("email@email.com"), Cron.Daily); IHostedService public class SendMailHostedService : IHostedService, IDisposable{ private readonly ILogger<SendMailHostedService> _logger; private Timer _timer; public SendMailHostedService(ILogger<SendMailHostedService> logger) { _logger = logger; } public Task StartAsync(CancellationToken stoppingToken) { _logger.LogInformation("Hosted Service running."); _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(5)); return Task.CompletedTask; } private void DoWork(object state) { //...Your stuff here _logger.LogInformation( "Timed Hosted Service is working. Count: {Count}", executionCount); } public Task StopAsync(CancellationToken stoppingToken) { _logger.LogInformation("Timed Hosted Service is stopping."); _timer?.Change(Timeout.Infinite, 0); return Task.CompletedTask; } public void Dispose() { _timer?.Dispose(); }}在您的startup.cs类中.在configure方法中添加它.services.AddHostedService<SendMailHostedService>();如果不需要将其作为后台作业托管在WebApp上,则可以创建Windows服务,该服务每天在需要的时间运行.查看此问题: Windows服务调度每天每天凌晨6:00运行要使用C#发送电子邮件,可以查看SmptClient类 https://docs.microsoft.com/zh-cn/dotnet/api/system.net.mail.smtpclient.send?view=netframework-4.8或使用可以为您完成的服务,例如SendGrid.关于第二个问题:实现接口时,您的类应在该接口上定义所有方法.此方法必须是公共的,返回相同的类型,具有相同的名称,并接收在您实现的接口上声明的相同参数.在您的特定情况下,您只是错过了方法名称.只需对其进行更改即可,如下所示.使用Quartz.net 3时,IJbo界面返回一个Task而不是void.因此,我更改了类SendMailJob以返回您现有方法的任务.public class SendMailJob : IJob{ public Task Execute(IJobExecutionContext context) { return Task.Factory.StartNew(() => SendEmail()); } public void SendMail() { 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); }}I've developed a C# web app MVC that gets some information from another site (Trello) through API calls, and allows the user to do some actions like printing an .xls file with all card details. Now, I want to implement a functionality that sends every day at a specific time in background a mail to my Gmail account with that Excel as an attachment. I want to implement that functionality in an external project but in the same solution, but I don't know how to do that, I heard about quartz.net but I didn't understand how it works and I don't know if that's the right solution. Can anyone help me and give me some tips?p.s. I can't host the appEDIT - New questionWhen I try to implement my background job with Quartz.Net I got this error that my class SendMailJob doesn't implement an interface member IJob.Execute.What i have to do?This is my jobs class:public class SendMailJob : IJob{ public void 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); }} 解决方案 If you really want to it as background job on a Asp.Net WebApp you should look into:Quartz.NetCreate a job to send e-mailpublic class SendMailJob : IJob{ public void Execute(IJobExecutionContext context) { ...Do your stuff; }}Then configure your job to execute daily// define the job and tie it to our SendMailJob classIJobDetail job = JobBuilder.Create<SendMailJob>() .WithIdentity("job1", "group1") .Build();// Trigger the job to run now, and then repeat every 24 hoursITrigger trigger = TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .StartNow() .WithSimpleSchedule(x => x .WithIntervalInHours(24) .RepeatForever()) .Build();HangFireRecurringJob.AddOrUpdate( () => YourSendMailMethod("email@email.com"), Cron.Daily);IHostedServicepublic class SendMailHostedService : IHostedService, IDisposable{ private readonly ILogger<SendMailHostedService> _logger; private Timer _timer; public SendMailHostedService(ILogger<SendMailHostedService> logger) { _logger = logger; } public Task StartAsync(CancellationToken stoppingToken) { _logger.LogInformation("Hosted Service running."); _timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(5)); return Task.CompletedTask; } private void DoWork(object state) { //...Your stuff here _logger.LogInformation( "Timed Hosted Service is working. Count: {Count}", executionCount); } public Task StopAsync(CancellationToken stoppingToken) { _logger.LogInformation("Timed Hosted Service is stopping."); _timer?.Change(Timeout.Infinite, 0); return Task.CompletedTask; } public void Dispose() { _timer?.Dispose(); }}In your startup.cs class. add this at configure method.services.AddHostedService<SendMailHostedService>();If do not need to host it as a backgroud job on your WebApp, then you can create a Windows Service that runs every day on the time you need.See this question: Windows service scheduling to run daily once a day at 6:00 AMTo send E-mails with C# you can take a look a SmptClient class https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.smtpclient.send?view=netframework-4.8Or use a service, like SendGrid, that can do it for you.EDIT:About your second question:When you implements an interface your class should have all methods defined on that interface.This methods needs to be public, return the same type, has the same name and receive the same parameters that was declared on the interface you implement.In your specific case, you just miss the method name. Just change it do Execute like below.EDIT: As you are using Quartz.net 3, the IJbo interface returns a Task and not void. So I changed the class SendMailJob to return a task of your existing method.public class SendMailJob : IJob{ public Task Execute(IJobExecutionContext context) { return Task.Factory.StartNew(() => SendEmail()); } public void SendMail() { 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); }} 这篇关于从ASP.NET MVC Web应用发送每日通知邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!