使用 Azure Scheduler / Postal / MVC 发送预定电子邮件
Send Scheduled Email using Azure Scheduler / Postal / MVC
我的项目托管在 Azure 上,我想每天早上向忘记完成我应用程序中某些任务的用户发送一封电子邮件。
我已经建立了我的电子邮件(使用邮政发送)。如果我 运行 函数本身,电子邮件将按预期发送。
我已将 Azure 调度程序配置为 运行 HTTPs 操作,get 方法,[https://www.example.com/Email/EmailReminder]。计划的作业报告成功,但没有电子邮件发出。
我以前不必这样做,所以我怀疑我的函数 > 调度程序作业之间缺少 link。我已经搜索了有关如何设置它的代码示例,但我还没有找到解决方案。调度程序期望我不给它什么?
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 = "test@test.com";
email.To = "test2@test2.com";
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 = "test@test.com";
email.To = "test2@test2.com";
email.NPFirstName = NPFirstName;
email.CheckOutCt = CheckOutCt;
email.Send();
}
}
}
它使用 TimerTrigger to fire at a given time (defined by the CRON expression),它比 HTTP POST 方法简单得多(在这种方法中您需要考虑 HTTP 超时)。
如果您对 CRON 表达式有疑问,请查看 CronMaker。
对于电子邮件发送和遵循 WebJobs SDK 示例,您可以将 SendGrid extension 与队列配对用于 解耦 ,这样您就可以拥有多个 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)
{
;
}
}
关于我为什么 Azure Scheduler 不发送我的电子邮件的问题,这是一个在 Azure 门户中解决的身份验证问题。
Matias的回答也很正确,也是我以后前进的方向。
我的项目托管在 Azure 上,我想每天早上向忘记完成我应用程序中某些任务的用户发送一封电子邮件。
我已经建立了我的电子邮件(使用邮政发送)。如果我 运行 函数本身,电子邮件将按预期发送。
我已将 Azure 调度程序配置为 运行 HTTPs 操作,get 方法,[https://www.example.com/Email/EmailReminder]。计划的作业报告成功,但没有电子邮件发出。
我以前不必这样做,所以我怀疑我的函数 > 调度程序作业之间缺少 link。我已经搜索了有关如何设置它的代码示例,但我还没有找到解决方案。调度程序期望我不给它什么?
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 = "test@test.com";
email.To = "test2@test2.com";
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 = "test@test.com";
email.To = "test2@test2.com";
email.NPFirstName = NPFirstName;
email.CheckOutCt = CheckOutCt;
email.Send();
}
}
}
它使用 TimerTrigger to fire at a given time (defined by the CRON expression),它比 HTTP POST 方法简单得多(在这种方法中您需要考虑 HTTP 超时)。
如果您对 CRON 表达式有疑问,请查看 CronMaker。
对于电子邮件发送和遵循 WebJobs SDK 示例,您可以将 SendGrid extension 与队列配对用于 解耦 ,这样您就可以拥有多个 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)
{
;
}
}
关于我为什么 Azure Scheduler 不发送我的电子邮件的问题,这是一个在 Azure 门户中解决的身份验证问题。
Matias的回答也很正确,也是我以后前进的方向。