Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

v2 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

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

_15
// using SendGrid's C# Library - https://github.com/sendgrid/sendgrid-csharp
_15
using System.Net.Http;
_15
using System.Net.Mail;
_15
_15
var myMessage = new SendGrid.SendGridMessage();
_15
myMessage.AddTo("test@sendgrid.com");
_15
myMessage.From = new EmailAddress("you@youremail.com", "First Last");
_15
myMessage.Subject = "Sending with SendGrid is Fun";
_15
myMessage.PlainTextContent= "and easy to do anywhere with C#.";
_15
_15
var transportWeb = new SendGrid.Web("SENDGRID_APIKEY");
_15
transportWeb.DeliverAsync(myMessage);
_15
// NOTE: If you're developing a Console Application,
_15
// 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).


_10
<system.net>
_10
<mailSettings>
_10
<smtp from="test@domain.com">
_10
<network host="smtp.sendgrid.net" password="<your_api_key>" userName="apikey" port="587" />
_10
</smtp>
_10
</mailSettings>
_10
</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)


_39
using System;
_39
using System.Net;
_39
using System.Net.Mail;
_39
using System.Net.Mime;
_39
_39
namespace SmtpMail
_39
{
_39
internal class Program
_39
{
_39
static void Main(string[] args)
_39
{
_39
using (MailMessage mailMsg = new MailMessage())
_39
{
_39
// API key
_39
string apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
_39
_39
// To
_39
mailMsg.To.Add(new MailAddress("to@example.com", "To Name"));
_39
_39
// From
_39
mailMsg.From = new MailAddress("from@example.com", "From Name");
_39
_39
// Subject and multipart/alternative Body
_39
mailMsg.Subject = "subject";
_39
string text = "text body";
_39
string html = @"<p>html body</p>";
_39
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
_39
mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));
_39
_39
// Init SmtpClient and send
_39
using (SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", 587))
_39
{
_39
smtpClient.Credentials = new NetworkCredential("apikey", apiKey);
_39
smtpClient.Send(mailMsg);
_39
}
_39
}
_39
}
_39
}
_39
}


Rate this page: