我认为您最好的选择是网络作业。我假设你已经有一个 Web App,所以如果你添加一个使用Webjob SDK的 Webjob ,你可以创建一个带有签名的函数:
public class Functions
{
public static void ProcessTimer([TimerTrigger("0 0 9 1/1 * ? *", RunOnStartup = true)]
TimerInfo info)
{
var remCheckOuts = // query code here
into grouped
select new Reminder
{
/// populate viewmodel
});
// send emails
foreach (var i in remCheckOuts)
{
string Full = i.Full;
string FirstName = i.FirstName;
var CheckOutCt = i.CheckOutCt;
dynamic email = new Email("emReminder");
email.FromAdd = "test@test.com";
email.To = "test2@test2.com";
email.NPFirstName = NPFirstName;
email.CheckOutCt = CheckOutCt;
email.Send();
}
}
}
它使用TimerTrigger在给定时间触发(由CRON 表达式定义),它比 HTTP POST 方法(您需要考虑 HTTP 超时)简单得多。
如果您在使用 CRON 表达式时遇到问题,请检查CronMaker。
对于电子邮件发送和遵循 WebJobs SDK 示例,您可以使用与 Queue 配对的SendGrid 扩展进行解耦,这样您就可以拥有多个 TimerTrigger 功能(例如,用于 X 用途的早安邮件、晚间邮件、用于 Y 用途的夜间电子邮件,月度报告)和一个发送所有邮件的功能:
public class MailNotification{
public string From {get;set;}
public string To {get;set;}
public string Subject {get;set;}
public string Body {get;set;}
}
public class Functions
{
public static void MorningMail([TimerTrigger("0 0 9 1/1 * ? *", RunOnStartup = true)]
TimerInfo info, [Queue]("mail") ICollector<MailNotification> mails)
{
var remCheckOuts = // query code here
into grouped
select new Reminder
{
/// populate viewmodel
});
// send emails
foreach (var i in remCheckOuts)
{
mails.Add(new MailNotification(){
To = "test2@test2.com",
From = "test@test.com",
Subject = "Whatever Subject you want",
Body = "construct the body here"
});
}
}
public static void EveningMail([TimerTrigger("0 0 18 1/1 * ? *", RunOnStartup = true)]
TimerInfo info, [Queue]("mail") ICollector<MailNotification> mails)
{
var remCheckOuts = // query code here
into grouped
select new Reminder
{
/// populate viewmodel
});
// send emails
foreach (var i in remCheckOuts)
{
mails.Add(new MailNotification(){
To = "test2@test2.com",
From = "test@test.com",
Subject = "Whatever Subject you want",
Body = "construct the body here"
});
}
}
public static void SendMails([QueueTrigger(@"mails")] MailNotification order,
[SendGrid(
To = "{To}",
From = "{From}",
Subject = "{Subject}",
Text = "{Body}")]
SendGridMessage message)
{
;
}
}