该库允许您通过 C# 和 .NET 快速轻松地使用 Twilio SendGrid Web API v3。
该库的版本 9.X.X+ 为所有 Twilio SendGrid Web API v3 端点提供全面支持,包括新的 v3 /mail/send。
有关此库的更新,请参阅我们的变更日志和版本。
感谢您一如既往的支持,谢谢!
从 Twilio SendGrid UI 获取您的 API 密钥。
通过将 Twilio SendGrid API 密钥存储在环境变量或 Web.config 中来管理它们。将数据和配置设置分开是一个很好的做法。这样您就可以更改 Twilio SendGrid API 密钥,而无需更改代码。此外,我们强烈建议不要将敏感数据直接存储在代码中。
使用 UI 设置环境变量:
使用CMD设置环境变量:
以下是以编程方式获取和设置 API 密钥的一些示例:
# Get Environment Variable
var apiKey = Environment . GetEnvironmentVariable ( " SENDGRID_API_KEY " ) ;
# Set Environment Variable
var setKey = Environment . SetEnvironmentVariable ( " SENDGRID_API_KEY " , " YOUR_API_KEY " ) ;
要在 C# 项目中使用 Twilio SendGrid,您可以直接从我们的 Github 存储库下载 Twilio SendGrid C# .NET 库,或者如果您安装了 NuGet 包管理器,则可以自动获取它们:
dotnet add package SendGrid
# use Twilio SendGrid with HttpClientFactory
dotnet add package SendGrid.Extensions.DependencyInjection
安装 Twilio SendGrid 库后,您可以在代码中包含对其的调用。有关示例实现,请参阅 .NET Core 示例和 .NET 4.5.2 示例文件夹。
请参阅 .csproj 文件。
以下是发送简单电子邮件所需的最少代码。使用此示例,并修改apiKey
、 from
和to
变量:
using System ;
using System . Threading . Tasks ;
using SendGrid ;
using SendGrid . Helpers . Mail ;
class Program
{
static async Task Main ( )
{
var apiKey = Environment . GetEnvironmentVariable ( " SENDGRID_API_KEY " ) ;
var client = new SendGridClient ( apiKey ) ;
var from = new EmailAddress ( " [email protected] " , " Example User " ) ;
var subject = " Sending with Twilio SendGrid is Fun " ;
var to = new EmailAddress ( " [email protected] " , " Example User " ) ;
var plainTextContent = " and easy to do anywhere, even with C# " ;
var htmlContent = " and easy to do anywhere, even with C# " ;
var msg = MailHelper . CreateSingleEmail ( from , to , subject , plainTextContent , htmlContent ) ;
var response = await client . SendEmailAsync ( msg ) . ConfigureAwait ( false ) ;
}
}
执行上述代码后, response.StatusCode
应为202
, to
的收件箱中应有一封电子邮件。您可以在 UI 中检查电子邮件的状态。或者,我们可以使用我们的事件 Webhook 将事件发布到您选择的 URL。这为您提供有关 Twilio SendGrid 处理您的电子邮件时发生的事件的数据。
对于更高级的情况,您可以使用以下最低必需设置自行构建 SendGridMessage 对象:
using System ;
using System . Threading . Tasks ;
using SendGrid ;
using SendGrid . Helpers . Mail ;
class Program
{
static async Task Main ( )
{
var apiKey = Environment . GetEnvironmentVariable ( " SENDGRID_API_KEY " ) ;
var client = new SendGridClient ( apiKey ) ;
var msg = new SendGridMessage ( )
{
From = new EmailAddress ( " [email protected] " , " DX Team " ) ,
Subject = " Sending with Twilio SendGrid is Fun " ,
PlainTextContent = " and easy to do anywhere, even with C# " ,
HtmlContent = " and easy to do anywhere, even with C# "
} ;
msg . AddTo ( new EmailAddress ( " [email protected] " , " Test User " ) ) ;
var response = await client . SendEmailAsync ( msg ) . ConfigureAwait ( false ) ;
}
}
您可以在此处找到所有电子邮件功能的示例。
using System ;
using System . Threading . Tasks ;
using SendGrid ;
class Program
{
static async Task Main ( )
{
var apiKey = Environment . GetEnvironmentVariable ( " SENDGRID_API_KEY " ) ;
var client = new SendGridClient ( apiKey ) ;
var queryParams = @"{'limit': 100}" ;
var response = await client . RequestAsync ( method : SendGridClient . Method . GET , urlPath : " suppression/bounces " ,
queryParams : queryParams ) . ConfigureAwait ( false ) ;
}
}
SendGrid.Extensions.DependencyInjection 是必需的
using System ;
using System . Threading . Tasks ;
using Microsoft . Extensions . DependencyInjection ;
using SendGrid ;
using SendGrid . Extensions . DependencyInjection ;
using SendGrid . Helpers . Mail ;
class Program
{
static async Task Main ( )
{
var services = ConfigureServices ( new ServiceCollection ( ) ) . BuildServiceProvider ( ) ;
var client = services . GetRequiredService < ISendGridClient > ( ) ;
var msg = new SendGridMessage ( )
{
From = new EmailAddress ( " [email protected] " , " Example User " ) ,
Subject = " Sending with Twilio SendGrid is Fun "
} ;
msg . AddContent ( MimeType . Text , " and easy to do anywhere, even with C# " ) ;
msg . AddTo ( new EmailAddress ( " [email protected] " , " Example User " ) ) ;
var response = await client . SendEmailAsync ( msg ) . ConfigureAwait ( false ) ;
}
private static IServiceCollection ConfigureServices ( IServiceCollection services )
{
services . AddSendGrid ( options =>
{
options . ApiKey = Environment . GetEnvironmentVariable ( " SENDGRID_API_KEY " ) ;
} ) ;
return services ;
}
}
var apiKey = Environment . GetEnvironmentVariable ( " SENDGRID_API_KEY " ) ;
var proxy = new WebProxy ( " http://proxy:1337 " ) ;
var client = new SendGridClient ( proxy , apiKey ) ;
或者当使用 DependencyInjection 时
services . AddSendGrid ( options =>
{
options . ApiKey = Environment . GetEnvironmentVariable ( " SENDGRID_API_KEY " ) ;
} )
. ConfigurePrimaryHttpMessageHandler ( _ => new HttpClientHandler ( )
{
Proxy = new WebProxy ( new Uri ( " http://proxy:1337 " ) ) ,
UseProxy = true
} ) ;
以下是常见 API 用例的一些示例,例如如何使用事务模板发送电子邮件。
我们鼓励为我们的图书馆做出贡献(您甚至可能会获得一些漂亮的礼物),请参阅我们的贡献指南了解详细信息。
快速链接:
请参阅我们的常见库问题故障排除指南。
sendgrid-csharp 由 Twilio SendGrid, Inc. 维护和资助。sendgrid-csharp 的名称和徽标是 Twilio SendGrid, Inc. 的商标。
如果您需要使用 SendGrid 的帮助,请查看 Twilio SendGrid 支持帮助中心。
麻省理工学院许可证 (MIT)