# .NET

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

Install the official `Mailblastr` NuGet package (`dotnet add package Mailblastr`) for typed access to the whole API — or POST JSON straight to `https://api.mailblastr.com/emails`. The example below uses the built-in `HttpClient`, which you can register in your app's DI container.

## Prerequisites

- A MailBlastr [API key](https://www.mailblastr.com/docs/api-keys/overview).
- A [verified domain](https://www.mailblastr.com/docs/domains/managing) to send from.

## 1. Register an HttpClient

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

```csharp
builder.Services.AddHttpClient("MailBlastr", client =>
{
    client.BaseAddress = new Uri("https://api.mailblastr.com/");
    client.DefaultRequestHeaders.Authorization =
        new System.Net.Http.Headers.AuthenticationHeaderValue(
            "Bearer",
            Environment.GetEnvironmentVariable("MAILBLASTR_API_KEY"));
});
```

## 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("MailBlastr");

        var message = new
        {
            from = "Acme <onboarding@yourdomain.com>",
            to = new[] { "delivered@example.com" },
            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.mailblastr.com/docs/api/emails-send) for the complete request body.
