Skip to contentSkip to navigationSkip to topbar
On this page

v2 API C# Code Example


(warning)

This documentation is for the SendGrid Web API v2

We will deprecate this version on February 10, 2025. To access all the latest features and upcoming developments, please see our v3 API. For assistance with transitioning, refer to our migration guide.

(information)
(information)

Info

The library does not officially support the V2 API, but you can use V2 with an older version of the library. For more information, see Continue Using V2 in C#(link takes you to an external page).


Using SendGrid's C# Library

using-sendgrids-c-library page anchor
1
// using SendGrid's C# Library - https://github.com/sendgrid/sendgrid-csharp
2
using System.Net.Http;
3
using System.Net.Mail;
4
5
var myMessage = new SendGrid.SendGridMessage();
6
myMessage.AddTo("test@sendgrid.com");
7
myMessage.From = new EmailAddress("you@youremail.com", "First Last");
8
myMessage.Subject = "Sending with SendGrid is Fun";
9
myMessage.PlainTextContent= "and easy to do anywhere with C#.";
10
11
var transportWeb = new SendGrid.Web("SENDGRID_APIKEY");
12
transportWeb.DeliverAsync(myMessage);
13
// NOTE: If you're developing a Console Application,
14
// use the following so that the API call has time to complete
15
// transportWeb.DeliverAsync(myMessage).Wait();

Using .NET's Built-in SMTP Library

using-nets-built-in-smtp-library page anchor

If you choose not to use SendGrid's client library you may use .NET's built in library.

If you are using ASP.NET, you can specify SMTP settings in web.config. Please note that your username should be "apikey" as specified in "Integrating with the SMTP API". Your password will be your SendGrid API key. For more information about SendGrid API keys, see our API Key documentation. We also have a Twilio blog post to help you learn "How to Set Environment Variables"(link takes you to an external page).

1
<system.net>
2
<mailSettings>
3
<smtp from="test@domain.com">
4
<network host="smtp.sendgrid.net" password="<your_api_key>" userName="apikey" port="587" />
5
</smtp>
6
</mailSettings>
7
</system.net>

This C# program will build a MIME email and send it through SendGrid. .NET already has built in libraries to send and receive emails. This example uses: .NET Mail(link takes you to an external page)

1
using System;
2
using System.Net;
3
using System.Net.Mail;
4
using System.Net.Mime;
5
6
namespace SmtpMail
7
{
8
internal class Program
9
{
10
static void Main(string[] args)
11
{
12
using (MailMessage mailMsg = new MailMessage())
13
{
14
// API key
15
string apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
16
17
// To
18
mailMsg.To.Add(new MailAddress("to@example.com", "To Name"));
19
20
// From
21
mailMsg.From = new MailAddress("from@example.com", "From Name");
22
23
// Subject and multipart/alternative Body
24
mailMsg.Subject = "subject";
25
string text = "text body";
26
string html = @"<p>html body</p>";
27
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
28
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
29
30
// Init SmtpClient and send
31
using (SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", 587))
32
{
33
smtpClient.Credentials = new NetworkCredential("apikey", apiKey);
34
smtpClient.Send(mailMsg);
35
}
36
}
37
}
38
}
39
}