c# - sendgrid向Azure功能中的多个收件人发送电子邮件失败

标签 c# azure azure-functions sendgrid sendgrid-api-v3

(此问题的完整版本请参阅 https://social.msdn.microsoft.com/Forums/en-US/20bb5b37-82af-4cf3-8a59-04e5f19572bc/send-email-to-multiple-recipients-using-sendgrid-failure?forum=AzureFunctions )

发送给单个收件人已成功。但在Azure功能中找不到发送给多个收件人或CC/BCC的方法。

尝试了多种格式,包括

{ "to": [{ "email": ["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7d17121513531912183d18051c100d1118531e1210" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e7d6b606a697c676a7a6b7d7a6760694e69636f6762206d6163" rel="noreferrer noopener nofollow">[email protected]</a>" ] }] }

这似乎是azure功能的限制。但还不确定哪里出了问题。请参阅下面的“绑定(bind)”,

{

"bindings": [

{

"name": "telemetryEvent",

"type": "serviceBusTrigger", 

"direction": "in",

"queueName": "threshold-email-queue",

"connection": "RootManageSharedAccessKey_SERVICEBUS",

"accessRights": "Manage"

},

{

"type": "sendGrid",

"name": "$return",

"apiKey": "SendGridKey",

"direction": "out",

"from": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d4c4f4e4d7e6c607d6168236e6260" rel="noreferrer noopener nofollow">[email protected]</a>",

"to": [{
"email": ["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfcbdacccb8effccded2cfd3da8e91dcd0d2" rel="noreferrer noopener nofollow">[email protected]</a>", "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="15617066612755667478657970273b767a78" rel="noreferrer noopener nofollow">[email protected]</a>" ]
}]

}

],
"disabled": false

}

最佳答案

我使用 HTTP 触发器完成了示例,但在此基础上您将能够使其与服务总线触发器一起使用。

我的函数.json:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "sendGrid",
      "name": "mails",
      "apiKey": "MySendGridKey",
      "direction": "out",
      "from":"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="75061418051910063513001b16011c1a1b065b161a18" rel="noreferrer noopener nofollow">[email protected]</a>"
    }
  ],
  "disabled": false
}

我的run.csx:

#r "SendGrid"

using System;
using System.Net;
using SendGrid.Helpers.Mail;

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, ICollector<Mail> mails)
{
    log.Info("C# HTTP trigger function processed a request.");

    Mail message = new Mail()
    {
        Subject = $"Hello world from the SendGrid C#!"
    };

    var personalization = new Personalization();
    personalization.AddTo(new Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23454c4c634142510d404c4e" rel="noreferrer noopener nofollow">[email protected]</a>"));  
    personalization.AddTo(new Email("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0a6c6565384a686b7824696567" rel="noreferrer noopener nofollow">[email protected]</a>")); 
    // you can add some more recipients here 

    Content content = new Content
    {
        Type = "text/plain",
        Value = $"Hello world!"
    };

    message.AddContent(content);  
    message.AddPersonalization(personalization); 
    mails.Add(message);

    return null;
}

我使用此源来构建我的示例: Azure Function SendGrid

关于c# - sendgrid向Azure功能中的多个收件人发送电子邮件失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50023458/

相关文章:

azure - 限制 Azure Function App 中的 Azure 存储队列处理

c# - 仅在页面回发时插入 javascript

c# - 在客户端将服务器端 OnClientclick 更改为 jQuery

c# - WebApi 中自动生成的 EF 代码无法在 Azure 上运行,但在测试时可以在本地运行

linq - LINQ to CosmosDB 中数组的交集

azure - 将 WebRole 部署到预先存在的 VM

c# - 如何使用我自己的由列表支持的 IQueryable 进行延迟加载

c# - 对 MSIL 感到困惑

c# - Azure Blob 存储 | AcquireLeaseAsync,同步等待直到锁释放

带有 KEDA 的 Azure Function 不会横向扩展超过 1 个 Pod