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#.
_15// using SendGrid's C# Library - https://github.com/sendgrid/sendgrid-csharp_15using System.Net.Http;_15using System.Net.Mail;_15_15var myMessage = new SendGrid.SendGridMessage();_15myMessage.AddTo("test@sendgrid.com");_15myMessage.From = new EmailAddress("you@youremail.com", "First Last");_15myMessage.Subject = "Sending with SendGrid is Fun";_15myMessage.PlainTextContent= "and easy to do anywhere with C#.";_15_15var transportWeb = new SendGrid.Web("SENDGRID_APIKEY");_15transportWeb.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();
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".
_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
_39using System;_39using System.Net;_39using System.Net.Mail;_39using System.Net.Mime;_39_39namespace 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}