NEW: Subscribe to email notifications for releases and breaking changes.
This library allows you to quickly and easily use the SendGrid Web API v3 via C# with .NET.
Version 9.X.X+ of this library provides full support for all SendGrid Web API v3 endpoints, including the new v3 /mail/send.
We want this library to be community driven and SendGrid led. We need your help to realize this goal. To help make sure we are building the right things in the right order, we ask that you create issues and pull requests or simply upvote or comment on existing issues or pull requests.
Please browse the rest of this README for further detail.
We appreciate your continued support, thank you!
- Installation
- Quick Start
- Usage
- Use Cases
- Announcements
- Roadmap
- How to Contribute
- Troubleshooting
- About
- .NET version 4.5.2 and higher
- .NET Core 1.0 and higher
- .NET Standard 1.3 support
- A SendGrid account, sign up for free to send up to 40,000 emails for the first 30 days or check out our pricing.
You can obtain your API Key within the SendGrid UI.
Update the development Environment with your SENDGRID_API_KEY. For example, in Windows 10, please review this thread.
To use SendGrid in your C# project, you can either download the SendGrid C# .NET libraries directly from our Github repository or, if you have the NuGet package manager installed, you can grab them automatically.
PM> Install-Package SendGrid
Once you have the SendGrid libraries properly referenced in your project, you can include calls to them in your code. For sample implementations, check the .NET Core Example and the .NET 4.5.2 Example folders.
- Please see the .nuspec file.
The following is the minimum needed code to send an simple email, just modify the apiKey
, from
and to
variables:
using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Threading.Tasks;
namespace Example
{
internal class Example
{
private static void Main()
{
Execute().Wait();
}
static async Task Execute()
{
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
var client = new SendGridClient(apiKey);
var from = new EmailAddress("test@example.com", "Example User");
var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress("test@example.com", "Example User");
var plainTextContent = "and easy to do anywhere, even with C#";
var htmlContent = "<strong>and easy to do anywhere, even with C#</strong>";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
}
}
}
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 via the UI here. Alternatively, we can post events to a URL of your choice via our Event Webhook about events that occur as SendGrid processes your email.
For more advanced cases, you can build the SendGridMessage object yourself, following is the minimum required settings:
using SendGrid;
using SendGrid.Helpers.Mail;
using System;
using System.Threading.Tasks;
namespace Example
{
internal class Example
{
private static void Main()
{
Execute().Wait();
}
static async Task Execute()
{
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
var client = new SendGridClient(apiKey);
var msg = new SendGridMessage()
{
From = new EmailAddress("test@example.com", "DX Team"),
Subject = "Sending with 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(new EmailAddress("test@example.com", "Test User"));
var response = await client.SendEmailAsync(msg);
}
}
}
You can find an example of all of the email features here.
using System;
using System.Threading.Tasks;
using SendGrid;
namespace Example
{
internal class Example
{
private static void Main()
{
Execute().Wait();
}
static async Task Execute()
{
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
var client = new SendGridClient(apiKey);
var queryParams = @"{
'limit': 100
}";
var response = await client.RequestAsync(method: SendGridClient.Method.GET,
urlPath: "suppression/bounces",
queryParams: queryParams);
}
}
}
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
var proxy = new WebProxy("http://proxy:1337");
var client = new SendGridClient(proxy, apiKey);
- SendGrid Docs
- Library Usage Docs
- Example Code - .NET Core
- Example Code - .NET 4.5.2+
- How-to: Migration from v2 to v3
- v3 Web API Mail Send Helper
Examples of common API use cases, such as how to send an email with a transactional template.
Please see our announcement regarding breaking changes. Your support is appreciated!
All updates to this library are documented in our CHANGELOG and releases. You may also subscribe to email release notifications for releases and breaking changes.
If you are interested in the future direction of this project, please take a look at our open issues and pull requests. We would love to hear your feedback.
We encourage contribution to our library (you might even score some nifty swag), please see our CONTRIBUTING guide for details.
Quick links:
Please see our troubleshooting guide for common library issues.
sendgrid-csharp is guided and supported by the SendGrid Developer Experience Team.
sendgrid-csharp is maintained and funded by SendGrid, Inc. The names and logos for sendgrid-csharp are trademarks of SendGrid, Inc.