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

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.

<div class="code-wrapper" id="wrapper_d9ada240753fe77a096865c71dea3330">
  <div class="code-buttons">
    <ul class="nav nav-tabs">
      <li class="pull-right">
        <a class="copycode code-button btn-mini" id="copy_d9ada240753fe77a096865c71dea3330">
          <i class="icon-copy"></i> Copy
        </a>
      </li>
      <li class="pull-right">
        <a class="expandcode code-button btn-mini" id="expand_d9ada240753fe77a096865c71dea3330">
          <i class="icon-fullscreen"></i> Fullscreen
        </a>
      </li>
    </ul>
  </div>
  <div class='bogus-wrapper'><notextile><figure class="code" id="code_d9ada240753fe77a096865c71dea3330">

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

<div class="code-wrapper" id="wrapper_67c32a046b026e015d8e95a5ab9ef2e8">
  <div class="code-buttons">
    <ul class="nav nav-tabs">
      <li class="pull-right">
        <a class="copycode code-button btn-mini" id="copy_67c32a046b026e015d8e95a5ab9ef2e8">
          <i class="icon-copy"></i> Copy
        </a>
      </li>
      <li class="pull-right">
        <a class="expandcode code-button btn-mini" id="expand_67c32a046b026e015d8e95a5ab9ef2e8">
          <i class="icon-fullscreen"></i> Fullscreen
        </a>
      </li>
    </ul>
  </div>
  <div class='bogus-wrapper'><notextile><figure class="code" id="code_67c32a046b026e015d8e95a5ab9ef2e8">

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); } } } }

Was this helpful? + Yes - No Feedback