无法使用 SendGrid 触发电子邮件以更改 blob 存储

Unable to use the SendGrid to trigger the email for changes in blob storage

我是 MS 函数的新手,正在尝试创建一个 C# 函数来在 Azure Blob 存储中添加新文件时触发电子邮件。

下面的代码示例:

#r "SendGrid"
using Microsoft.Extensions.Logging;
using System;
using System.Text.RegularExpressions;
using SendGrid.Helpers.Mail;


public static void Run(string myBlob, string filename, ILogger log, out Mail message)
{
    var email = Regex.Match(myBlob, @"^email\:\ (.+)$", RegexOptions.Multiline).Groups[1].Value;
    log.LogInformation($"Got order from {email}\n License File Name: {filename} ");

    message = new Mail();
    var personalization = new Personalization();
    personalization.AddTo(new Email(email));
    message.AddPersonalization(personalization);

    Attachment attach = new Attachment();
    var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(myBlob);
    attach.Content = System.Convert.ToBase64String(plainTextBytes);
    attach.Type = "text/plain";
    attach.Filename = "license.lic";
    attach.Disposition = "attachment";
    attach.ContentId = "License File";
    message.AddAttachment(attach);

    var msgContent = new Content("text/html", "Your license file attached");
    message.AddContent(msgContent);
    message.Subject = "Thanks for your order";
    message.From = new Email("test3@test.com");  

}

我是运行功能版本~2(检查了应用程序设置变量)。

我的问题是为什么我收到邮件参数错误(邮件消息)?

我上次检查时 SendGrid 安装正确。

以下是显示编译错误的日志:(我不知道为什么它无法识别方法中的 Mail 参数)

2018-11-26T05:46:57.013 [Error] run.csx(8,73): error CS0246: The type or namespace name 'Mail' could not be found (are you missing a using directive or an assembly reference?)

Mail 是 v1 函数中使用的 class,适用于 v8 SendGrid SDK。而对于 v2 功能,SDK 是 v9,我们使用 SendGridMessageMail 不再可用,因此出现错误。

如果您确定安装了 Storage 和 SendGrid 扩展,请尝试下面的代码。

#r "SendGrid"

using System.Text.RegularExpressions;
using SendGrid.Helpers.Mail;

public static void Run(string myBlob, string filename, ILogger log, out SendGridMessage message)
{  
    var email = Regex.Match(myBlob, @"^email\:\ (.+)$", RegexOptions.Multiline).Groups[1].Value;
    log.LogInformation($"Got order from {email}\n License File Name: {filename} ");

    message = new SendGridMessage();
    message.AddTo(new EmailAddress(email));
    var base64Content = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(myBlob));

    message.AddAttachment(
        "license.lic",
        base64Content,
        "text/plain",
        "attachment",
        "License File"
    );

    message.AddContent("text/html", "Your license file attached");
    message.Subject = "Thanks for your order";
    message.From = new EmailAddress("test3@test.com");
}

function.json

{
  "bindings": [
    {
      "name": "myBlob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "yourContainerName/{filename}",
      "connection": "AzureWebJobsStorage"
    },
    {
      "type": "sendGrid",
      "name": "message",
      "apiKey": "YOURSENDAPIKEYAPPSETTING",
      "direction": "out"
    }
  ]
}