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.
We recommend using SendGrid C#, our client library, available on GitHub, with full documentation.
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#.
1// using SendGrid's C# Library - https://github.com/sendgrid/sendgrid-csharp2using System.Net.Http;3using System.Net.Mail;45var myMessage = new SendGrid.SendGridMessage();6myMessage.AddTo("test@sendgrid.com");7myMessage.From = new EmailAddress("you@youremail.com", "First Last");8myMessage.Subject = "Sending with SendGrid is Fun";9myMessage.PlainTextContent= "and easy to do anywhere with C#.";1011var transportWeb = new SendGrid.Web("SENDGRID_APIKEY");12transportWeb.DeliverAsync(myMessage);13// NOTE: If you're developing a Console Application,14// use the following so that the API call has time to complete15// transportWeb.DeliverAsync(myMessage).Wait();
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".
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
1using System;2using System.Net;3using System.Net.Mail;4using System.Net.Mime;56namespace SmtpMail7{8internal class Program9{10static void Main(string[] args)11{12using (MailMessage mailMsg = new MailMessage())13{14// API key15string apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");1617// To18mailMsg.To.Add(new MailAddress("to@example.com", "To Name"));1920// From21mailMsg.From = new MailAddress("from@example.com", "From Name");2223// Subject and multipart/alternative Body24mailMsg.Subject = "subject";25string text = "text body";26string html = @"<p>html body</p>";27mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));28mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));2930// Init SmtpClient and send31using (SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", 587))32{33smtpClient.Credentials = new NetworkCredential("apikey", apiKey);34smtpClient.Send(mailMsg);35}36}37}38}39}