Skip to content

Repository files navigation

SendGrid Logo

Test and DeployNuGetMIT licensedTwitter FollowGitHub contributorsOpen Source Helpers

Announcements

  • Send SMS messages with Twilio.

Overview

This library allows you to quickly and easily use the Twilio SendGrid Web API v3 via C# with .NET.

Version 9.X.X+ of this library provides full support for all Twilio SendGrid Web API v3 endpoints, including the new v3 /mail/send.

For updates to this library, see our CHANGELOG and releases.

We appreciate your continued support, thank you!

Table of Contents

Installation

Prerequisites

  • .NET Framework 4.0+
  • .NET Core 1.0+
  • .NET Standard 1.3+
  • A Twilio SendGrid account, sign up for free to send up to 40,000 emails for the first 30 days, then send 100 emails/day free forever or check out our pricing.

Obtain an API Key

Grab your API Key from the Twilio SendGrid UI.

Setup Environment Variables to Manage Your API Key

Manage your Twilio SendGrid API Keys by storing them in Environment Variables or in Web.config. It is a good practice to keep your data and configuration settings separate. This way you can change your Twilio SendGrid API key without changing your code. Also, we strongly advise against storing sensitive data directly in your code.

Setup Environment Variables using the UI:

  1. Press Win+R and run SystemPropertiesAdvanced
  2. Click on Environment Variables
  3. Click New in user variables section
  4. Type SENDGRID_API_KEY in the name. (Make sure this name matches the name of the key in your code)
  5. Type actual API Key in the value
  6. Restart the IDE and you're done!

Setup Environment Variables using CMD:

  1. Run CMD as administrator
  2. set SENDGRID_API_KEY="YOUR_API_KEY"

Here are a few examples to get and set API Keys programmatically:

# GetEnvironmentVariablevar apiKey =Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
# SetEnvironmentVariablevar setKey =Environment.SetEnvironmentVariable("SENDGRID_API_KEY","YOUR_API_KEY");

Install Package

To use Twilio SendGrid in your C# project, you can either download the Twilio SendGrid C# .NET libraries directly from our Github repository or if you have the NuGet package manager installed, you can grab them automatically:

dotnet add package SendGrid
# use Twilio SendGrid with HttpClientFactory
dotnet add package SendGrid.Extensions.DependencyInjection

Once you have the Twilio SendGrid library installed, you can include calls to it in your code. For sample implementations, see the .NET Core Example and the .NET 4.5.2 Example folders.

Dependencies

Please see the .csproj file.

Quick Start

Hello Email

The following is the minimum needed code to send a simple email. Use this example, and modify the apiKey, from and to variables:

usingSystem;usingSystem.Threading.Tasks;usingSendGrid;usingSendGrid.Helpers.Mail;classProgram{staticasyncTaskMain(){varapiKey=Environment.GetEnvironmentVariable("SENDGRID_API_KEY");varclient=newSendGridClient(apiKey);varfrom=newEmailAddress("test@example.com","Example User");varsubject="Sending with Twilio SendGrid is Fun";varto=newEmailAddress("test@example.com","Example User");varplainTextContent="and easy to do anywhere, even with C#";varhtmlContent="<strong>and easy to do anywhere, even with C#</strong>";varmsg=MailHelper.CreateSingleEmail(from,to,subject,plainTextContent,htmlContent);varresponse=awaitclient.SendEmailAsync(msg).ConfigureAwait(false);}}

After executing the above code, response.StatusCode should be 202 and you should have an email in the inbox of the to recipient. You can check the status of your email in the UI. Alternatively, we can post events to a URL of your choice using our Event Webhook. This gives you data about the events that occur as Twilio SendGrid processes your email.

For more advanced cases, you can build the SendGridMessage object yourself with these minimum required settings:

usingSystem;usingSystem.Threading.Tasks;usingSendGrid;usingSendGrid.Helpers.Mail;classProgram{staticasyncTaskMain(){varapiKey=Environment.GetEnvironmentVariable("SENDGRID_API_KEY");varclient=newSendGridClient(apiKey);varmsg=newSendGridMessage(){From=newEmailAddress("test@example.com","DX Team"),Subject="Sending with Twilio SendGrid is Fun",PlainTextContent="and easy to do anywhere, even with C#",HtmlContent="<strong>and easy to do anywhere, even with C#</strong>"};msg.AddTo(newEmailAddress("test@example.com","Test User"));varresponse=awaitclient.SendEmailAsync(msg).ConfigureAwait(false);}}

You can find an example of all the email features here.

General v3 Web API Usage

usingSystem;usingSystem.Threading.Tasks;usingSendGrid;classProgram{staticasyncTaskMain(){varapiKey=Environment.GetEnvironmentVariable("SENDGRID_API_KEY");varclient=newSendGridClient(apiKey);varqueryParams=@"{'limit': 100}";varresponse=awaitclient.RequestAsync(method:SendGridClient.Method.GET,urlPath:"suppression/bounces",queryParams:queryParams).ConfigureAwait(false);}}

HttpClientFactory + Microsoft.Extensions.DependencyInjection

SendGrid.Extensions.DependencyInjection is required

usingSystem;usingSystem.Threading.Tasks;usingMicrosoft.Extensions.DependencyInjection;usingSendGrid;usingSendGrid.Extensions.DependencyInjection;usingSendGrid.Helpers.Mail;classProgram{staticasyncTaskMain(){varservices=ConfigureServices(newServiceCollection()).BuildServiceProvider();varclient=services.GetRequiredService<ISendGridClient>();varmsg=newSendGridMessage(){From=newEmailAddress("test@example.com","Example User"),Subject="Sending with Twilio SendGrid is Fun"};msg.AddContent(MimeType.Text,"and easy to do anywhere, even with C#");msg.AddTo(newEmailAddress("test@example.com","Example User"));varresponse=awaitclient.SendEmailAsync(msg).ConfigureAwait(false);}privatestaticIServiceCollectionConfigureServices(IServiceCollectionservices){services.AddSendGrid(options =>{options.ApiKey=Environment.GetEnvironmentVariable("SENDGRID_API_KEY");});returnservices;}}

Web Proxy

varapiKey=Environment.GetEnvironmentVariable("SENDGRID_API_KEY");varproxy=newWebProxy("http://proxy:1337");varclient=newSendGridClient(proxy,apiKey);

Or when using DependencyInjection

services.AddSendGrid(options =>{options.ApiKey=Environment.GetEnvironmentVariable("SENDGRID_API_KEY");}).ConfigurePrimaryHttpMessageHandler(_ =>newHttpClientHandler(){Proxy=newWebProxy(newUri("http://proxy:1337")),UseProxy=true});

Usage

Use Cases

Here are some examples of common API use cases, such as how to send an email with a transactional template.

How to Contribute

We encourage contribution to our library (you might even score some nifty swag), please see our CONTRIBUTING guide for details.

Quick links:

Troubleshooting

Please see our troubleshooting guide for common library issues.

About

sendgrid-csharp is maintained and funded by Twilio SendGrid, Inc. The names and logos for sendgrid-csharp are trademarks of Twilio SendGrid, Inc.

Support

If you need help using SendGrid, please check the Twilio SendGrid Support Help Center.

License

The MIT License (MIT)

About

The Official Twilio SendGrid C#, .NetStandard, .NetCore API Library

Topics

Resources

Code of conduct

Contributing

Stars

1.1k stars

Watchers

255 watching

Forks

Releases

Packages

Used by

Contributors

Languages