简体   繁体   English

在asp.net中自动发送发票电子邮件

[英]Send invoice email automatically in asp.net

Right now our asp.net website is sending invoice manually, Is it possible send email to customers automatically every 5 minutes? 现在,我们的asp.net网站正在手动发送发票,是否可以每5分钟自动将电子邮件发送给客户? All invoices is using software to put in SQL Server Database, after put in database, they need to send to different customer email address with attached PDF file. 所有发票都使用软件放入SQL Server数据库,放入数据库后,它们需要发送带有附件PDF文件的不同客户电子邮件地址。 That means the code needs collect new invoices information from database every 5 minutes and send it. 这意味着代码需要每5分钟从数据库中收集新的发票信息并发送一次。 Then create a log file include the successful and unsuccessful invoice sent to accounting. 然后创建一个日志文件,其中包含已发送到会计的成功和不成功的发票。

Can I use task scheduler or windows services? 我可以使用任务计划程序或Windows服务吗?

Create a page called CronMail.aspx and send the emails in the load event (I explain the idea using some pseudocode): 创建一个名为CronMail.aspx的页面,并在load事件中发送电子邮件(我使用一些伪代码解释了这个想法):

void Page_Load(object source, EventArgs e)
{
     //1. Check that more than 5 minutes happened before last sent
     DateTime lastSent = ...;
     if (DateTime.Now().Substract(lastSent).Minutes < 5)
          return;

     //2. Get your invoices and PDF files
     var dataToSent = ...;

     //3. Send your invoices
     foreach(var item in dataToSent) 
     {
          //4. Save sent date every time to avoid resending in case the whole operation takes more than 5 minutes 
          saveSentTime();

         //5. Finally send your data
     }
}

Then you can use the Windows tasks scheduler to call your URL every 5 minutes: http://your_website/CronMail.aspx 然后,您可以使用Windows任务计划程序每5分钟调用一次URL: http://your_website/CronMail.aspx

Here is an example: https://superuser.com/questions/447368/set-windows-scheduler-to-schedule-open-a-website-webpage-or-the-bookmark (use chrome instead of iexplorer) 这是一个示例: https : //superuser.com/questions/447368/set-windows-scheduler-to-schedule-open-a-website-webpage-or-the-bookmark (使用chrome代替iexplorer)

The Hangfire library is a complete back-end job scheduler and background processor that runs on top of more recent versions of ASP.NET. Hangfire库是一个完整的后端作业计划程序和后台处理器,可在最新版本的ASP.NET上运行。 It runs on its own (there's a special IIS app pool configuration required - "AlwaysRunning") without the need for an external web-page ping, or task scheduler. 它可以自行运行(需要特殊的IIS应用程序池配置-“ AlwaysRunning”),而无需外部网页ping或任务计划程序。 You can schedule lots of jobs to run as often as once per minute. 您可以安排许多作业每分钟运行一次。

I've had lots of success with it. 我已经取得了很多成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM