我的项目托管在Azure上,我希望每天早晨向忘记了在应用程序中完成某些任务的用户发送电子邮件。
我已经建立了电子邮件(使用邮政发送)。如果我自己运行该功能,则电子邮件将按预期发送。
我已将Azure调度程序配置为运行HTTPs操作,get方法[ https://www.example.com/Email/EmailReminder]。预定的作业报告为成功,但是没有电子邮件发送出去。
我以前没有必要这样做,所以我怀疑我的函数>调度程序作业之间缺少链接。我已经搜索了有关如何进行设置的代码示例,但是还没有找到解决方案。调度程序期望我不给它什么?
public void EmailReminder()
{
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 = "[email protected]";
email.To = "[email protected]";
email.NPFirstName = NPFirstName;
email.CheckOutCt = CheckOutCt;
email.Send();
}
}
我认为您最好的选择是Webjob。我假设您已经有一个Web应用程序,因此,如果添加使用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 = "[email protected]";
email.To = "[email protected]";
email.NPFirstName = NPFirstName;
email.CheckOutCt = CheckOutCt;
email.Send();
}
}
}
它使用TimerTrigger在给定的时间触发(由CRON表达式定义),它比HTTP POST方法简单得多(在该方法中,您需要考虑HTTP超时)。
为了发送电子邮件并遵循WebJobs SDK示例,您可以将SendGrid扩展名与Queue配对使用以进行解耦,这样您就可以拥有多个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 = "[email protected]",
From = "[email protected]",
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 = "[email protected]",
From = "[email protected]",
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)
{
;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句