該程式庫可讓您透過 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 的協助,請查看 Twilio SendGrid 支援協助中心。
麻省理工學院許可證 (MIT)