# .NET

> Send your first email from .NET using HttpClient against the SendPing API.

Install the official `SendPing` NuGet package for typed access to the whole API — [Send emails with .NET & C#](https://www.sendping.co/docs/integrations/dotnet) is the guide for that — or POST JSON straight to `https://www.sendping.co/api/emails`. The example below uses the built-in `HttpClient`, which you can register in your app's DI container.

## Prerequisites

- A SendPing [API key](https://www.sendping.co/docs/api-keys/overview).
- A [verified domain](https://www.sendping.co/docs/domains/managing) to send from.
- .NET 8.0 or newer if you install the official `SendPing` package — it targets `net8.0` only, so a `net6.0`/`net7.0` or .NET Framework project cannot restore it.

## 1. Register an HttpClient

In the startup of your application, register a named or typed `HttpClient` pointed at the SendPing API, reading the key from configuration/environment.

```csharp
builder.Services.AddHttpClient("SendPing", client =>
{
    client.BaseAddress = new Uri("https://www.sendping.co/api/");
    client.DefaultRequestHeaders.Authorization =
        new System.Net.Http.Headers.AuthenticationHeaderValue(
            "Bearer",
            Environment.GetEnvironmentVariable("SENDPING_API_KEY"));

    // REQUIRED: HttpClient sends no User-Agent of its own, and the API answers a
    // request without one with 403 validation_error before it authenticates.
    client.DefaultRequestHeaders.UserAgent.ParseAdd("my-app/1.0");
});
```

## 2. Send emails using HTML

Resolve the client and POST the email body as JSON.

```csharp
using System.Net.Http.Json;

public class FeatureImplementation
{
    private readonly IHttpClientFactory _factory;

    public FeatureImplementation(IHttpClientFactory factory)
    {
        _factory = factory;
    }

    public async Task Execute()
    {
        var client = _factory.CreateClient("SendPing");

        var message = new
        {
            from = "Acme <onboarding@yourdomain.com>",
            to = new[] { "delivered@test.sendping.co" },
            subject = "hello world",
            html = "<strong>it works!</strong>",
        };

        var response = await client.PostAsJsonAsync("emails", message);
        response.EnsureSuccessStatusCode();

        var result = await response.Content.ReadFromJsonAsync<EmailResult>();
        Console.WriteLine(result?.Id);
    }

    private record EmailResult(string Id);
}
```

> **Note:** This works the same in an ASP.NET Minimal API or MVC app. See [Send an email](https://www.sendping.co/docs/api/emails-send) for the complete request body.
