# v2 API C# Code Example

> \[!WARNING]
>
> To access all the latest features and upcoming developments, please see our [v3 API](/docs/sendgrid/api-reference). For assistance with transitioning, refer to our [migration guide](/docs/sendgrid/for-developers/migration-guides/v2-to-v3-api).

> \[!NOTE]
>
> We recommend using SendGrid C#, our client library, [available on
> GitHub](https://github.com/sendgrid/sendgrid-csharp), with full documentation.

> \[!NOTE]
>
> 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#](https://github.com/sendgrid/sendgrid-csharp/blob/main/TROUBLESHOOTING.md#v2).

## Using SendGrid's C# Library

```csharp
// using SendGrid's C# Library - https://github.com/sendgrid/sendgrid-csharp
using System.Net.Http;
using System.Net.Mail;

var myMessage = new SendGrid.SendGridMessage();
myMessage.AddTo("test@sendgrid.com");
myMessage.From = new EmailAddress("you@example.com", "First Last");
myMessage.Subject = "Sending with SendGrid is Fun";
myMessage.PlainTextContent= "and easy to do anywhere with C#.";

var transportWeb = new SendGrid.Web("SENDGRID_APIKEY");
transportWeb.DeliverAsync(myMessage);
// NOTE: If you're developing a Console Application,
// use the following so that the API call has time to complete
// transportWeb.DeliverAsync(myMessage).Wait();
```

## 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. Your username should be "apikey" as specified in ["Integrating with the SMTP API"](/docs/sendgrid/for-developers/sending-email/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](/docs/sendgrid/ui/account-and-settings/api-keys). We also have a Twilio blog post to help you learn ["How to Set Environment Variables"](https://www.twilio.com/blog/how-to-set-environment-variables-html).

```xml
<system.net>
  <mailSettings>
    <smtp from="test@domain.com">
      <network host="smtp.sendgrid.net" password="<your_api_key>" userName="apikey" 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](https://learn.microsoft.com/en-us/dotnet/api/system.net.mail?view=net-6.0)

```csharp
using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;

namespace SmtpMail
{
  internal class Program
  {
    static void Main(string[] args)
    {
      using (MailMessage mailMsg = new MailMessage())
      {
        // API key
        string apiKey = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");

        // 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
        using (SmtpClient smtpClient = new SmtpClient("smtp.sendgrid.net", 587))
        {
          smtpClient.Credentials = new NetworkCredential("apikey", apiKey);
          smtpClient.Send(mailMsg);
        }
      }
    }
  }
}
```
