sendgrid 向 Azure 函数中的多个收件人发送电子邮件失败
sendgrid send email to multiple recipients in Azure function failed
发送给单个收件人成功。但是在 Azure 函数中找不到发送给多个收件人或 CC/BCC 的方法。
尝试了多种格式,包括
{ "to": [{ "email": ["john.doe@example.com", "sendgridtesting@gmail.com" ] }] }
这似乎是 azure 函数的限制。但还不确定哪里出了问题。请参考下面的"bindings",
{
"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": "ABC@sample.com",
"to": [{
"email": ["test1@sample1.com", "test2@sample2.com" ]
}]
}
],
"disabled": false
}
我用 HTTP 触发器做了我的示例,但基于此,您将能够使其与服务总线触发器一起工作。
我的function.json:
{
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
]
},
{
"type": "sendGrid",
"name": "mails",
"apiKey": "MySendGridKey",
"direction": "out",
"from":"samples@functions.com"
}
],
"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("foo@bar.com"));
personalization.AddTo(new Email("foo2@bar.com"));
// 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
感谢 Tamás Huj,该函数现在可以完成这项工作。所以我post详细的解决方法供大家参考。
{
"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": "ABC@sample.com"
}
],
"disabled": false
}
然后将run.csx写成:
#r "SendGrid"
#r "Newtonsoft.Json"
using System;
using SendGrid.Helpers.Mail;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
public static Mail Run(string telemetryEvent, TraceWriter log)
{
var telemetry = JsonConvert.DeserializeObject<Telemetry>(telemetryEvent);
Mail message = new Mail()
{
Subject = "Write your own subject"
};
var personalization = new Personalization();
personalization.AddBcc(new Email("sample1@test.com"));
personalization.AddTo(new Email("sample2@test.com"));
//add more Bcc,cc and to here as needed
Content content = new Content
{
Type = "text/plain",
Value = $"The temperature value is{temperature.Temperature}."
};
message.AddContent(content);
message.AddPersonalization(personalization);
return message;
}
public class Telemetry
{
public float Temperature { get; set; }
}
发送给单个收件人成功。但是在 Azure 函数中找不到发送给多个收件人或 CC/BCC 的方法。
尝试了多种格式,包括
{ "to": [{ "email": ["john.doe@example.com", "sendgridtesting@gmail.com" ] }] }
这似乎是 azure 函数的限制。但还不确定哪里出了问题。请参考下面的"bindings",
{
"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": "ABC@sample.com",
"to": [{
"email": ["test1@sample1.com", "test2@sample2.com" ]
}]
}
],
"disabled": false
}
我用 HTTP 触发器做了我的示例,但基于此,您将能够使其与服务总线触发器一起工作。
我的function.json:
{
"bindings": [
{
"authLevel": "function",
"name": "req",
"type": "httpTrigger",
"direction": "in",
"methods": [
"get",
"post"
]
},
{
"type": "sendGrid",
"name": "mails",
"apiKey": "MySendGridKey",
"direction": "out",
"from":"samples@functions.com"
}
],
"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("foo@bar.com"));
personalization.AddTo(new Email("foo2@bar.com"));
// 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
感谢 Tamás Huj,该函数现在可以完成这项工作。所以我post详细的解决方法供大家参考。
{
"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": "ABC@sample.com"
}
],
"disabled": false
}
然后将run.csx写成:
#r "SendGrid"
#r "Newtonsoft.Json"
using System;
using SendGrid.Helpers.Mail;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Host;
using Newtonsoft.Json;
public static Mail Run(string telemetryEvent, TraceWriter log)
{
var telemetry = JsonConvert.DeserializeObject<Telemetry>(telemetryEvent);
Mail message = new Mail()
{
Subject = "Write your own subject"
};
var personalization = new Personalization();
personalization.AddBcc(new Email("sample1@test.com"));
personalization.AddTo(new Email("sample2@test.com"));
//add more Bcc,cc and to here as needed
Content content = new Content
{
Type = "text/plain",
Value = $"The temperature value is{temperature.Temperature}."
};
message.AddContent(content);
message.AddPersonalization(personalization);
return message;
}
public class Telemetry
{
public float Temperature { get; set; }
}