Quick setup examples

.NET

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

Install the official Mailblastr NuGet package for typed access to the whole API — Send emails with .NET & C# is the guide for that — or POST JSON straight to https://www.mailblastr.com/api/emails. The example below uses the built-in HttpClient, which you can register in your app's DI container.

Prerequisites

  • A MailBlastr API key.
  • A verified domain to send from.
  • .NET 8.0 or newer if you install the official Mailblastr 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 MailBlastr API, reading the key from configuration/environment.

builder.Services.AddHttpClient("MailBlastr", client =>
{
    client.BaseAddress = new Uri("https://www.mailblastr.com/api/");
    client.DefaultRequestHeaders.Authorization =
        new System.Net.Http.Headers.AuthenticationHeaderValue(
            "Bearer",
            Environment.GetEnvironmentVariable("MAILBLASTR_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.

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@mailblastr.dev" },
            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);
}
This works the same in an ASP.NET Minimal API or MVC app. See Send an email for the complete request body.