我已经在Azure函数中浏览了SendGrid邮件程序的以下代码,
[FunctionName("SendEmail")]
public static async void Run(
[ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] Message
email,
[SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")]
IAsyncCollector<SendGridMessage> messageCollector)
{
var emailObject = JsonConvert.DeserializeObject<OutgoingEmail>
(Encoding.UTF8.GetString(email.Body));
var message = new SendGridMessage();
message.AddTo(emailObject.To);
message.AddContent("text/html", emailObject.Body);
message.SetFrom(new EmailAddress(emailObject.From));
message.SetSubject(emailObject.Subject);
await messageCollector.AddAsync(message);
}
public class OutgoingEmail
{
public string To { get; set; }
public string From { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
如果我的理解是正确的,这将向消息队列“ myqueue”发送消息,但是我很困惑,我是否需要向队列“ myqueue”写一个自定义侦听器以从队列中获取消息并向调用“ client.SendEmailAsync”发送邮件或SendGrid bindng背后有一些魔术来自动提取邮件并触发邮件?
答案 0 :(得分:1)
您的理解不太正确。
这将侦听队列中的消息。
然后调用SendGrid API发送传入的消息。此函数期望从队列输入的是JSON序列化消息:
[ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] Message email