Skip to contentSkip to navigationSkip to topbar
On this page

v3 API C# Code Example


(information)

Info

We recommend using SendGrid C#, our client library, available on GitHub(link takes you to an external page), with full documentation.

(information)

Info

Do you have an API Key(link takes you to an external page) yet? If not, go get one. You're going to need it to integrate!


Using SendGrid's C# Library

using-sendgrids-c-library page anchor
1
// using SendGrid's C# Library
2
// https://github.com/sendgrid/sendgrid-csharp
3
using SendGrid;
4
using SendGrid.Helpers.Mail;
5
using System;
6
using System.Threading.Tasks;
7
8
namespace Example
9
{
10
internal class Example
11
{
12
private static void Main()
13
{
14
Execute().Wait();
15
}
16
17
static async Task Execute()
18
{
19
var apiKey = Environment.GetEnvironmentVariable("NAME_OF_THE_ENVIRONMENT_VARIABLE_FOR_YOUR_SENDGRID_KEY");
20
var client = new SendGridClient(apiKey);
21
var from = new EmailAddress("test@example.com", "Example User");
22
var subject = "Sending with SendGrid is Fun";
23
var to = new EmailAddress("test@example.com", "Example User");
24
var plainTextContent = "and easy to do anywhere with C#.";
25
var htmlContent = "<strong>and easy to do anywhere with C#.</strong>";
26
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
27
var response = await client.SendEmailAsync(msg);
28
}
29
}
30
}