Watch Aspire live streamsDocumentaciónPrueba Aspire

Connect to Posta with the Aspire integration

Esta página aún no está disponible en tu idioma.

⭐ Community Toolkit

The Community Toolkit Posta client configures an HttpClient for Posta’s application-facing HTTP API and registers IPostaClient in dependency injection. Add the Posta resource to your AppHost first. For its PostgreSQL, Redis, and container settings, see Set up Posta in the AppHost.

The hosting resource exposes Host, Port, and Uri. The connection string uses Endpoint=http://{host}:{port}. When the AppHost resource is named posta, reference it from the consuming project to provide the ConnectionStrings:posta value.

Configure an API key or access token separately. The client sends the selected credential as a bearer token. Keep credentials in a secret provider rather than in source control.

Install the 📦 CommunityToolkit.Aspire.Posta package in the consuming project:

.NET CLI — Add CommunityToolkit.Aspire.Posta package
dotnet add package CommunityToolkit.Aspire.Posta

Call AddPostaClient with the connection name used by the AppHost. Configure the API key from your application’s configuration:

Program.cs
builder.AddPostaClient(
"posta",
settings => settings.ApiKey = builder.Configuration["Posta:ApiKey"]);

The IPostaClient provides clients for email, templates, subscriber lists, health, and application information. Inject the interface into your service:

MailService.cs
using CommunityToolkit.Aspire.Posta.Clients;
using CommunityToolkit.Aspire.Posta.Models.Emails;
public sealed class MailService(IPostaClient posta)
{
public Task<SendAnEmailResponse?> SendAsync(CancellationToken cancellationToken) =>
posta.Emails.SendAnEmailAsync(
new SendAnEmailRequest
{
From = "Acme <hello@example.com>",
To = ["user@example.com"],
Subject = "Hello",
Html = "<h1>Hello!</h1>",
},
cancellationToken);
}

Register keyed clients when the app connects to more than one Posta instance:

Program.cs
builder.AddKeyedPostaClient(
"transactional-mail",
settings => settings.ApiKey = builder.Configuration["Posta:TransactionalApiKey"]);
builder.AddKeyedPostaClient(
"marketing-mail",
settings => settings.AccessToken = builder.Configuration["Posta:MarketingAccessToken"]);

Resolve a client with its key:

MailServices.cs
public sealed class MailServices(
[FromKeyedServices("transactional-mail")] IPostaClient transactional,
[FromKeyedServices("marketing-mail")] IPostaClient marketing)
{
}

For more information, see .NET dependency injection: Keyed services.

AddPostaClient("posta") reads the connection string named posta. A standalone app can configure the endpoint directly:

appsettings.json
{
"ConnectionStrings": {
"posta": "Endpoint=http://localhost:9000"
}
}

When the AppHost references Posta, it supplies the endpoint connection string to the app.

The client uses the Aspire:Posta:Client configuration section by default. For a named client, it also reads Aspire:Posta:Client:{connectionName}. Use configuration providers for endpoint, API key, or access token settings:

appsettings.json
{
"Aspire": {
"Posta": {
"Client": {
"Endpoint": "http://localhost:9000",
"ApiKey": ""
}
}
}
}

Load secret credentials from a secret provider, environment variable, or deployment configuration instead of committing them to this file.

Pass an Action<PostaClientSettings> to configure client settings in code:

Program.cs
builder.AddPostaClient(
"posta",
settings =>
{
settings.ApiKey = builder.Configuration["Posta:ApiKey"];
settings.Timeout = TimeSpan.FromSeconds(30);
});