当进程完成时从 azure functions 发送电子邮件

send an email from azure functions when a process finishes

我有一个设备可以将文本形式的数据发送到 AZURE 上的 blob,一旦 blob 接收到数据,它就会触发 azure 函数中的一个函数,该函数基本上是由 c++ 代码制成的可执行文件,当它完成时会生成存储在其他 blob

中的另一个文本文件

这是一个非常简单的操作。但是现在我想在每次函数成功通过时收到一封电子邮件,我在网上搜索过但是教程非常混乱或者没有解决这个简单的任务。

我确实用 c++ 开发了可执行文件,但我从别人那里继承了 azure 函数,而且我对 azure 的经验为零(我是电气工程师,不是计算机科学)。 azure函数是用C#写的,我只需要一个指南。

提前致谢!!

使用可以将 SendGrid 输出绑定添加到您的 C# Azure 函数。 function.json 中的绑定看起来像这样:

{
    "name": "mail",
    "type": "sendGrid",
    "direction": "out",
    "apiKey" : "MySendGridKey"
}

和这样的函数体:

#r "SendGrid"
using SendGrid.Helpers.Mail;

public static void Run(string input, out string yourExistingOutput, out Mail message)
{
    // Do the work you already do

    message = new Mail
    {        
        Subject = "Your Subject"          
    };

    var personalization = new Personalization();
    personalization.AddTo(new Email("recipient@contoso.com"));   

    Content content = new Content
    {
        Type = "text/plain",
        Value = "Email Body"
    };
    message.AddContent(content);
    message.AddPersonalization(personalization);
}

了解 SendGrid and SendGrid bindings

我有一个类似的问题,Mikhail 的解决方案帮我解决了。在我的例子中,我希望静态 运行 方法是异步的,这意味着我不能使用 out 参数修饰符。我的解决方案略有不同,因为它是一个计时器触发器,并且是使用 Visual Studio 和 NuGet 包 Microsoft.Azure.Webjobs.Extensions.SendGrid v2.1.0 实现的。

    [FunctionName("MyFunction")]
    public static async Task Run(
        [TimerTrigger("%TimerInterval%")]TimerInfo myTimer,
        [SendGrid] IAsyncCollector<Mail> messages,
        TraceWriter log)
    {
        log.Info($"C# Timer trigger function started execution at: {DateTime.Now}");

        // Do the work you already do...

        log.Info($"C# Timer trigger function finished execution at: {DateTime.Now}");

        var message = new Mail();
        message.From = new Email("from@email.com");
        var personalization = new Personalization();
        personalization.AddTo(new Email("to@email.com"));
        personalization.Subject = "Azure Function Executed Succesfully";
        message.AddPersonalization(personalization);

        var content = new Content
        {
            Type = "text/plain",
            Value = $"Function ran at {DateTime.Now}",
        };
        message.AddContent(content);
        await messages.AddAsync(message);
    }

此解决方案使用了 Zain Rivzi 对 如何将输出值绑定到异步 ​​Azure 函数的回答? SendGrid Web API v3 quick start guide.

答案可以稍微简化一下:

using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using SendGrid.Helpers.Mail;

public static class ExtractArchiveBlob
{
    [FunctionName("MyFunction")]
    public static async Task RunAsync(string input, 
                                      [SendGrid(ApiKey = "SendGridApiKey")] 
                                      IAsyncCollector<SendGridMessage> messageCollector)
    {
            var message = new SendGridMessage();

            message.AddContent("text/plain", "Example content");
            message.SetSubject("Example subject");
            message.SetFrom("from@email.com");
            message.AddTo("to@email.com");

            await messageCollector.AddAsync(message);
   }
}

其中 SendGridApiKey 是按住发送网格 api 键的应用设置。