We recommend using SendGrid C#, our client library, available on Github, with full documentation.

Using SendGrid’s C# Library

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// using SendGrid's C# Library - https://github.com/sendgrid/sendgrid-csharp
using System;
using System.Net;
using System.Net.Mail;
using SendGridMail;
using SendGridMail.Transport;

SendGrid mail = SendGrid.GetInstance();
mail.From = new MailAddress("you@youremail.com");
mail.AddTo("test@sendgrid.com");
mail.Subject = "Sending with SendGrid is Fun";
mail.Text = "and easy to do anywhere, even with C#";

var credentials = new NetworkCredential(api_user, api_key);
var transportWeb = new Web(credentials);
transportWeb.Deliver(mail);

Using .NET’s Built-in SMTP Library

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.

1
2
3
4
5
6
7
<system.net>
  <mailSettings>
    <smtp from="test@domain.com">
      <network host="smtp.sendgrid.net" password="password" userName="username" port="587" />
    </smtp>
  </mailSettings>
</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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;

namespace SmtpMail
{
  class Program
  {        
    static void Main()
    {
      try
      {
        MailMessage mailMsg = new MailMessage();

        // To
        mailMsg.To.Add(new MailAddress("to@example.com", "To Name"));

        // From
        mailMsg.From = new MailAddress("from@example.com", "From Name");

        // Subject and multipart/alternative Body
        mailMsg.Subject = "subject";
        string text = "text body";
        string html = @"<p>html body</p>";
        mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain));
        mailMsg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html));

        // Init SmtpClient and send
        SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", Convert.ToInt32(587));
        System.Net.NetworkCredential credentials = new System.Net.NetworkCredential("username@domain.com", "yourpassword");
        smtpClient.Credentials = credentials;

        smtpClient.Send(mailMsg);
      }
        catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
      }

    }
  }
}