gpt4 book ai didi

c# - 使用 Azure Scheduler/Postal/MVC 发送预定电子邮件

转载 作者:行者123 更新时间:2023-11-30 19:56:06 27 4
gpt4 key购买 nike

我的项目托管在 Azure 上,我想每天早上向忘记完成应用程序中某些任务的用户发送一封电子邮件。

我已经建立了电子邮件(使用邮政发送)。如果我运行该函数本身,电子邮件将按预期发送。

我已将 Azure 调度程序配置为运行 HTTPs 操作、获取方法,[ 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a4d0c1d7d0e4d0c1d7d08ac7cbc9" rel="noreferrer noopener nofollow">[email protected]</a>";
email.To = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1b6f7e686f295b6f7e686f2935787476" rel="noreferrer noopener nofollow">[email protected]</a>";
email.NPFirstName = NPFirstName;
email.CheckOutCt = CheckOutCt;
email.Send();
}
}

最佳答案

我认为你最好的选择是网络工作。我假设您已经有一个 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ffbeafcfbcffbeafcfba1ece0e2" rel="noreferrer noopener nofollow">[email protected]</a>";
email.To = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="deaabbadaaec9eaabbadaaecf0bdb1b3" rel="noreferrer noopener nofollow">[email protected]</a>";
email.NPFirstName = NPFirstName;
email.CheckOutCt = CheckOutCt;
email.Send();
}
}
}

它使用TimerTrigger在给定时间触发(由 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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e397869097d1a397869097d1cd808c8e" rel="noreferrer noopener nofollow">[email protected]</a>",
From = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee9a8b9d9aae9a8b9d9ac08d8183" rel="noreferrer noopener nofollow">[email protected]</a>",
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 = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5723322423651723322423657934383a" rel="noreferrer noopener nofollow">[email protected]</a>",
From = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3d7c6d0d7e3d7c6d0d78dc0ccce" rel="noreferrer noopener nofollow">[email protected]</a>",
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)
{
;
}
}

关于c# - 使用 Azure Scheduler/Postal/MVC 发送预定电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34884577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com