【问题标题】:How do I use SendGrid in an Azure Function?如何在 Azure 函数中使用 SendGrid?
【发布时间】:2020-10-08 16:58:39
【问题描述】:

我试图了解我应该如何在 Azure 门户中开发的 Azure 函数中使用 SendGrid。正如 Google 所建议的那样,我一直在从多个角度进行研究,但似乎有许多过时的方法,我无法通过 Azure 门户中可用的方法来解决。下面是我当前发送电子邮件的基本迭代,我直接从 SendGrid 博客 (https://sendgrid.com/blog/using-sendgrid-with-azure-functions-to-send-mobile-app-survey-data) 中提取。我已经看到使用 project.json 文件来确保安装 SendGrid 包的引用,但我无法在我的函数中创建这样的文件,我只能操作 run .csxfunction.json 文件。下面是我的代码和我遇到的错误。我错过了什么?

#r "Newtonsoft.Json"
#r "SendGrid"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using SendGrid.Helpers.Mail;

public static void Run(HttpRequest req, ILogger log, out Message message)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    message = new Mail {
        Subject = "Test Notification"
    };

    string requestBody = new StreamReader(req.Body).ReadToEnd();

    Content content = new Content {
        Type = "text/html",
        Value = requestBody
    };
    
    message.AddContent(content);
}

错误:
[错误] run.csx(10,58):错误 CS0246:找不到类型或命名空间名称“消息”(您是否缺少 using 指令或程序集引用?)
[错误] run.csx(14,19):错误 CS0246:找不到类型或命名空间名称“邮件”(您是否缺少 using 指令或程序集引用?)

【问题讨论】:

    标签: azure azure-functions sendgrid azure-function-app


    【解决方案1】:

    也许尝试使用SendGridMessage message = new SendGridMessage()???

    我首先建议您尝试以下方法以确保您的 SendGrid 流程正常工作:

    1. 创建“发送网格”通知类型的新函数。您将能够看到有关如何以这种方式使用 SendGrid 的正确 C#。看到这张图片

    然后您可以更新现有代码以使其正常工作。 我能够创建一个示例 HTTP 触发函数来发送 SendGrid 消息。我附上了function.json和run.csx供你参考。

    函数.json

    {
      "bindings": [
        {
          "authLevel": "function",
          "name": "req",
          "type": "httpTrigger",
          "direction": "in",
          "methods": [
            "get",
            "post"
          ]
        },
        {
          "name": "$return",
          "apiKey": "sendgridkey",
          "to": "receiver@test.com",
          "from": "sender@test.com",
          "direction": "out",
          "type": "sendGrid"
        }
      ]
    }
    

    这里的“sendgridkey”是一个以 SendGrid API 键为值的应用配置。我为电子邮件添加了虚拟值。

    我的run.csx如下:

    #r "SendGrid"
    #r "Newtonsoft.Json"
    
    using System.Net;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Primitives;
    using Newtonsoft.Json;
    using SendGrid.Helpers.Mail;
    
    public static async Task<SendGridMessage> Run(HttpRequest req, ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
    
        string name = req.Query["name"];
    
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
        name = name ?? data?.name;
    
        
        SendGridMessage message = new SendGridMessage()
        {
            Subject = $"Thanks for your order (#{name})!"
        };
    
        message.AddContent("text/plain", "Your order is being processed now!!");
        return message;
    }
    

    【讨论】:

    • 密钥使用SendGridMessage message = new SendGridMessage(),感谢您的帮助。
    猜你喜欢
    • 2018-12-19
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    相关资源
    最近更新 更多