与SendGrid一起用作输出绑定时,Azure函数中的错误处理 [英] Error handling in Azure function when used with SendGrid as output binding

查看:69
本文介绍了与SendGrid一起用作输出绑定时,Azure函数中的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个C#Azure函数,输出绑定到SendGrid。当消息到达ServiceBus主题时,将触发此Azure功能。我对消息进行操作,最后返回SendGridMessage。在这里,SendGrid负责发送实际的
电子邮件。



但在整个场景中,Azure功能无法知道电子邮件交付或失败。是否有任何方法或钩子可以提供上述细节?

解决方案

AFAIK,没有使用SendGrid的内置方式绑定但您可以使用函数中的自定义sendgrid客户端获取结果,如下所示:

 var client = new SendGridClient(" ENTER-YOUR-API-KEY-此处"); 
var msg = new SendGridMessage();

msg.SetFrom(new EmailAddress(" a@gmail.com"," blah!blah!"));

var recipients = new List< EmailAddress>
{
new EmailAddress(" a@gmail.com"),
new EmailAddress(" b@gmail.com"),
new EmailAddress(" c @ gmail.com")
};
msg.AddTos(收件人);

msg.SetSubject("来自Azure的邮件");

msg.AddContent(MimeType.Text,"这只是一个简单的测试消息!");
msg.AddContent(MimeType.Html,"< p>这只是一个简单的测试消息!< / p>");
var response = await client.SendEmailAsync(msg);


希望这会有所帮助!!


I have a C# Azure function with output binding to SendGrid. This Azure Function gets triggered when a message arrives in a ServiceBus topic. I operate on message and finally returns SendGridMessage. Hereon, SendGrid is responsible for sending out the actual email.

But in this whole scenario, Azure function does not get to know if the email is delivered or failed. Is there any way or hook available which can give me the aforementioned details ?

解决方案

AFAIK, there is no built-in way using SendGrid binding but you can get the results using custom sendgrid client in your function as shown below:

var client = new SendGridClient("ENTER-YOUR-API-KEY-HERE");
            var msg = new SendGridMessage();

            msg.SetFrom(new EmailAddress("a@gmail.com", "blah! blah!"));

            var recipients = new List<EmailAddress>
                {
                    new EmailAddress("a@gmail.com"),
                    new EmailAddress("b@gmail.com"),
                    new EmailAddress("c@gmail.com")
                };
            msg.AddTos(recipients);

            msg.SetSubject("Mail from Azure");

            msg.AddContent(MimeType.Text, "This is just a simple test message!");
            msg.AddContent(MimeType.Html, "<p>This is just a simple test message!</p>");
            var response = await client.SendEmailAsync(msg);

Hope this helps!!


这篇关于与SendGrid一起用作输出绑定时,Azure函数中的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆