You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function中使用SendGrid发送多收件人邮件失败

解决Azure Function中SendGrid多收件人/CC/BCC发送失败的问题

我来帮你搞定这个问题!你碰到的情况根本不是Azure Function的限制,而是SendGrid的请求体格式写错了,这是很常见的小坑~

问题根源

你之前尝试的格式{ "to": [{ "email": ["john.doe@example.com", "sendgridtesting@gmail.com" ] }] }不符合SendGrid的API规范:SendGrid要求to/cc/bcc字段是对象数组,每个对象里只能包含单个email值,不能把多个邮箱塞进同一个email数组里。

正确的多收件人格式

直接给你正确的JSON示例,照着写就行:

{
  "to": [
    {"email": "john.doe@example.com"},
    {"email": "sendgridtesting@gmail.com"}
  ],
  "subject": "多收件人测试邮件",
  "content": [{"type": "text/plain", "value": "这是一封发给多个收件人的测试邮件"}]
}

如何添加CC/BCC

同样遵循对象数组的规则,直接在请求体里加入cc和bcc字段即可:

{
  "to": [{"email": "john.doe@example.com"}],
  "cc": [{"email": "cc-user@example.com"}],
  "bcc": [{"email": "bcc-user@example.com"}],
  "subject": "带抄送的测试邮件",
  "content": [{"type": "text/plain", "value": "这封邮件包含了CC和BCC收件人"}]
}

Azure Function中的代码示例(以JavaScript为例)

如果你用的是SendGrid输出绑定,在函数代码里要构造正确的message对象:

module.exports = async function (context, req) {
    context.bindings.message = {
        to: [
            { email: "john.doe@example.com" },
            { email: "sendgridtesting@gmail.com" }
        ],
        cc: [{ email: "cc-user@example.com" }],
        subject: "测试邮件",
        content: [{ type: "text/plain", value: "测试内容" }]
    };
    context.done();
};

对应的function.json绑定配置要确保正确:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": ["get", "post"]
    },
    {
      "type": "sendGrid",
      "name": "message",
      "direction": "out",
      "apiKey": "SendGridApiKey"
    }
  ]
}

只要按照这个格式调整,多收件人、CC/BCC功能就能正常工作啦,和Azure Function本身没有关系,只是SendGrid的API格式要求而已~

内容的提问来源于stack exchange,提问作者Joseph

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:23:20