Azure Functions SendGrid 绑定Azure Functions SendGrid bindings
本文介绍如何使用 Azure Functions 中的 SendGrid 绑定发送电子邮件。This article explains how to send email by using SendGrid bindings in Azure Functions. Azure Functions 支持 SendGrid 的输出绑定。Azure Functions supports an output binding for SendGrid.
此参考信息面向 Azure Functions 开发人员。This is reference information for Azure Functions developers. Azure Functions 的新手请从以下资源入手:If you're new to Azure Functions, start with the following resources:
- 创建你的第一个函数:C#、JavaScript、Java。Create your first function: C#, JavaScript, Java.
- Azure Functions 开发人员参考。Azure Functions developer reference.
- 特定于语言的参考:C#、C# 脚本、F#、Java 或 JavaScript。Language-specific reference: C#, C# script, F#, Java, or JavaScript.
- Azure Functions 触发器和绑定概念。Azure Functions triggers and bindings concepts.
- 在本地对 Azure Functions 进行编码和测试。Code and test Azure Functions locally.
包 - Functions 1.xPackages - Functions 1.x
Microsoft.Azure.WebJobs.Extensions.SendGrid NuGet 包 2.x 版本中提供了 SendGrid 绑定。The SendGrid bindings are provided in the Microsoft.Azure.WebJobs.Extensions.SendGrid NuGet package, version 2.x. azure-webjobs-sdk-extensions GitHub 存储库中提供了此包的源代码。Source code for the package is in the azure-webjobs-sdk-extensions GitHub repository.
下表说明了如何在每个开发环境中添加对此绑定的支持。The following table tells how to add support for this binding in each development environment.
开发环境Development environment | 添加支持To add support in Functions 1.xFunctions 1.x |
---|---|
本地开发 - C# 类库Local development - C# class library | 安装包Install the package |
本地开发 - C# 脚本、JavaScript、F#Local development - C# script, JavaScript, F# | 自动Automatic |
门户开发Portal development | 自动Automatic |
包 - Functions 2.x 及更高版本Packages - Functions 2.x and higher
Microsoft.Azure.WebJobs.Extensions.SendGrid NuGet 包 3.x 版本中提供了 SendGrid 绑定。The SendGrid bindings are provided in the Microsoft.Azure.WebJobs.Extensions.SendGrid NuGet package, version 3.x. azure-webjobs-sdk-extensions GitHub 存储库中提供了此包的源代码。Source code for the package is in the azure-webjobs-sdk-extensions GitHub repository.
使用以下方法在首选开发环境中添加支持。Add support in you preferred development environment using the following methods.
开发环境Development environment | 应用程序类型Application type | 添加支持To add support |
---|---|---|
Visual StudioVisual Studio | C# 类库C# class library | 安装 NuGet 包Install the NuGet package |
Visual Studio CodeVisual Studio Code | 基于核心工具Based on core tools | 注册扩展捆绑包Register the extension bundle 建议安装 Azure Tools 扩展。Installing the Azure Tools extension is recommended. |
任何其他编辑器/IDEAny other editor/IDE | 基于核心工具Based on core tools | 注册扩展捆绑包Register the extension bundle |
Azure 门户Azure Portal | 仅在门户中联机Online only in portal | 在添加绑定时安装Installs when adding a binding 请参阅更新扩展以更新现有绑定扩展,而无需重新发布函数应用。See Update your extensions to update existing binding extensions without having to republish your function app. |
示例Example
以下示例演示使用服务总线队列触发器和 SendGrid 输出绑定的 C# 函数。The following example shows a C# function that uses a Service Bus queue trigger and a SendGrid output binding.
同步Synchronous
using SendGrid.Helpers.Mail;
using System.Text.Json;
...
[FunctionName("SendEmail")]
public static void Run(
[ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] Message email,
[SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")] out SendGridMessage message)
{
var emailObject = JsonSerializer.Deserialize<OutgoingEmail>(Encoding.UTF8.GetString(email.Body));
message = new SendGridMessage();
message.AddTo(emailObject.To);
message.AddContent("text/html", emailObject.Body);
message.SetFrom(new EmailAddress(emailObject.From));
message.SetSubject(emailObject.Subject);
}
public class OutgoingEmail
{
public string To { get; set; }
public string From { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
异步Asynchronous
using SendGrid.Helpers.Mail;
using System.Text.Json;
...
[FunctionName("SendEmail")]
public static async Task Run(
[ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] Message email,
[SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")] IAsyncCollector<SendGridMessage> messageCollector)
{
var emailObject = JsonSerializer.Deserialize<OutgoingEmail>(Encoding.UTF8.GetString(email.Body));
var message = new SendGridMessage();
message.AddTo(emailObject.To);
message.AddContent("text/html", emailObject.Body);
message.SetFrom(new EmailAddress(emailObject.From));
message.SetSubject(emailObject.Subject);
await messageCollector.AddAsync(message);
}
public class OutgoingEmail
{
public string To { get; set; }
public string From { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
如果在应用设置中指定了名为“AzureWebJobsSendGridApiKey”的 API 密钥,则可以不设置特性的 ApiKey
属性。You can omit setting the attribute's ApiKey
property if you have your API key in an app setting named "AzureWebJobsSendGridApiKey".
特性和注释Attributes and annotations
在 C# 类库中,使用 SendGrid 特性。In C# class libraries, use the SendGrid attribute.
有关可以配置的特性属性的信息,请参阅配置。For information about attribute properties that you can configure, see Configuration. 下面是某个方法签名中的 SendGrid
特性示例:Here's a SendGrid
attribute example in a method signature:
[FunctionName("SendEmail")]
public static void Run(
[ServiceBusTrigger("myqueue", Connection = "ServiceBusConnection")] OutgoingEmail email,
[SendGrid(ApiKey = "CustomSendGridKeyAppSettingName")] out SendGridMessage message)
{
...
}
有关完整示例,请参阅 C# 示例。For a complete example, see C# example.
配置Configuration
下表列出了在 function.json 文件和 SendGrid
特性/注释中可用的绑定配置属性。The following table lists the binding configuration properties available in the function.json file and the SendGrid
attribute/annotation.
function.json 属性function.json property | 特性/注释属性Attribute/annotation property | 说明Description | 可选Optional |
---|---|---|---|
typetype | 不适用n/a | 必须设置为 sendGrid 。Must be set to sendGrid . |
否No |
directiondirection | 不适用n/a | 必须设置为 out 。Must be set to out . |
否No |
namename | 不适用n/a | 在请求或请求正文的函数代码中使用的变量名称。The variable name used in function code for the request or request body. 只有一个返回值时,此值为 $return 。This value is $return when there is only one return value. |
否No |
apiKeyapiKey | ApiKeyApiKey | 包含 API 密钥的应用设置的名称。The name of an app setting that contains your API key. 如果未设置,默认应用设置名称为“AzureWebJobsSendGridApiKey”。If not set, the default app setting name is AzureWebJobsSendGridApiKey. | 否No |
toto | 如果To | 收件人的电子邮件地址。The recipient's email address. | 是Yes |
fromfrom | 从From | 发件人的电子邮件地址。The sender's email address. | 是Yes |
subjectsubject | 使用者Subject | 电子邮件主题。The subject of the email. | 是Yes |
texttext | 文本Text | 电子邮件内容。The email content. | 是Yes |
在绑定中可能会定义可选属性的默认值,并以编程方式添加或重写这些值。Optional properties may have default values defined in the binding and either added or overridden programmatically.
在本地进行开发时,应用设置将取 local.settings.json 文件的值。When you're developing locally, app settings go into the local.settings.json file.
host.json 设置host.json settings
本部分介绍版本 2.x 及更高版本中可用于此绑定的全局配置设置。This section describes the global configuration settings available for this binding in versions 2.x and higher. 下面的示例 host.json 文件仅包含此绑定的 2.x 版及更高版本设置。The example host.json file below contains only the version 2.x+ settings for this binding. 若要详细了解 2.x 版及更高版本中的全局配置设置,请参阅 Azure Functions 的 host.json 参考。For more information about global configuration settings in versions 2.x and beyond, see host.json reference for Azure Functions.
备注
有关 Functions 1.x 中 host.json 的参考,请参阅 Azure Functions 1.x 的 host.json 参考。For a reference of host.json in Functions 1.x, see host.json reference for Azure Functions 1.x.
{
"version": "2.0",
"extensions": {
"sendGrid": {
"from": "Azure Functions <samples@functions.com>"
}
}
}
属性Property | 默认Default | 说明Description |
---|---|---|
fromfrom | 不适用n/a | 所有函数的发件人电子邮件地址。The sender's email address across all functions. |