2

我的项目托管在 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 = "test@test.com";
                email.To = "test2@test2.com";
                email.NPFirstName = NPFirstName;
                email.CheckOutCt = CheckOutCt;
                email.Send();
      } 
}
4

2 回答 2

7

我认为您最好的选择是网络作业。我假设你已经有一个 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)
    {
        ;
    }
}
于 2016-01-19T19:15:29.717 回答
1

关于我为什么 Azure 调度程序没有发送我的电子邮件的问题,这是在 Azure 门户中解决的身份验证问题。

马蒂亚斯的回答也是正确的,也是我以后要走的方向。

于 2016-01-19T22:29:19.767 回答