Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据用户发送定期邮件';ASP.net中的设置?_C#_Asp.net_Email_Sendmail - Fatal编程技术网

C# 根据用户发送定期邮件';ASP.net中的设置?

C# 根据用户发送定期邮件';ASP.net中的设置?,c#,asp.net,email,sendmail,C#,Asp.net,Email,Sendmail,在我的web应用程序中,我希望根据用户自己预先选择的时间段为用户发送邮件。例如:每3天向用户01发送一封HTML邮件,每20天向用户02发送一封HTML邮件。 我该怎么做呢?有什么资源吗?我可以通过我的应用程序将邮件从我的Gmail帐户发送到我表格中的任何邮件地址,但我不知道如何在一段时间内自动发送 帮帮我 您需要一些可以定期为您运行作业的东西,比如cron守护进程或windows任务调度器 基本上,你有定期的工作开始,做任何你需要的邮件处理 如果您可以创建一个windows服务,使其在需要发

在我的web应用程序中,我希望根据用户自己预先选择的时间段为用户发送邮件。
例如:每3天向用户01发送一封HTML邮件,每20天向用户02发送一封HTML邮件。

我该怎么做呢?有什么资源吗?我可以通过我的应用程序将邮件从我的Gmail帐户发送到我表格中的任何邮件地址,但我不知道如何在一段时间内自动发送

帮帮我

您需要一些可以定期为您运行作业的东西,比如cron守护进程或windows任务调度器

基本上,你有定期的工作开始,做任何你需要的邮件处理

如果您可以创建一个windows服务,使其在需要发送下一批邮件之前基本处于休眠状态,那么您也可以从代码中执行此操作


最简单的方法是编写定期运行的任务调度器或cron作业。通过这种方式,您只需要一小段代码来处理邮件发送部分,然后您只需安排它每小时或每天运行一次,或者根据需要运行一次。

我认为有一种解决方案:

1-如果您有用户表,则必须在sql db的用户表中添加2列,在第一列中添加上次发送给用户的电子邮件的日期,第二列中包含发送该用户电子邮件的时间段,例如: LastEmailSentDate日期时间 SendEmailPeriod int

2-在应用程序代码中编写一个函数,将上次发送电子邮件的最后日期和发送电子邮件的时间段进行比较

//这里是函数代码

public void CompareLastSentDate()
{
   // lets assume that you bring the data for the db using Sqdatareader reader
   //get the field from the LastEmailSentDate field in the  database as i mention before
   DateTime LastEmailSentDate = Convert.ToDate(reader["DatePeriod"]) 

   // get the field from the SendEmailPeriod of the user field from database
   int sendEmailPeriod =  Convert.Toint32(reader["SendEmailPeriod"]) 

   // now you have the date before the period of day ex: before 3 days depend on user
   DateTime DatePeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, (DateTime.Now.Day - sendEmailPeriod ));

   // if the last email send is before period of day that mean u have to send an email again
   if(LastEmailSentDate.Day <= DatePeriod.Day)
   {
      // sent the email to the user
   }

}
public void CompareLastSentDate()
{
//让我们假设您使用Sqdatareader为db带来数据
//如前所述,从数据库中的LastEmailSentDate字段获取该字段
DateTime LastEmailSentDate=Convert.ToDate(读取器[“DatePeriod”])
//从数据库的用户字段的SendEmailPeriod中获取字段
int sendmailperiod=Convert.Toint32(读卡器[“sendmailperiod”])
//现在您有了日期前的一段日子ex:before 3天取决于用户
DateTime DatePeriod=新的日期时间(DateTime.Now.Year,DateTime.Now.Month,(DateTime.Now.Day-sendEmailPeriod));
//如果最后一封电子邮件是在一天中的某个时段之前发送的,这意味着你必须再次发送电子邮件

if(LastEmailSentDate.DayIIS托管ASP.NET的好处在于,IIS(默认情况下)会根据应用程序池本身的设置定期回收您的应用程序池

当您的应用程序池启动时(可能至少每天一次,特别是在允许其空闲的情况下,即大多数活动为9-5的业务应用程序),将启动Global.asax中的应用程序启动事件处理程序。这可用于您的定期任务

现在,您不一定希望在应用程序启动处理程序中同步运行此电子邮件发送,因为对我来说,此消息传递功能似乎不是启动的核心,但无论如何,请使用此事件处理程序作为定期管理的简单方法


要发送async,您应该使用async委托作为示例。

我找到了解决方案。
根据我的搜索,我们有3种方法来处理此问题:

  • (或)
  • 但在方法1和2中,我们应该访问服务器,并且我们需要专用的托管服务器,例如在其上安装WinService。因此,它在我们通常使用的共享主机空间中不起作用。因此,我找到了最佳方法,如您所见:

    三,

    上面的链接是一个非常好的解决方案。因此,不需要对我们的web应用程序进行任何修改。

    Hai

    看看

    Quartz.NET是一个功能齐全的开源作业调度系统,可以从最小的应用程序到大型企业系统使用


    Quartz.NET是一个非常合适的开源Java作业调度框架,Quartz.Quartz.NET支持集群和数据库持久化,并且具有强大的工具,可以使用类似cron的表达式、间隔触发器和排除建议来调度作业。

    但我们应该称之为CompareLastSentDate()定期运行。你知道这只是给用户发送邮件(如果我在用户中循环你建议的方法)一次,下一个周期呢?事实上,这个函数应该每天调用,检查今天是否发送邮件。但是谢谢你的回答。现在检查我的回答,我想你也会找到解决方法