Skip to content
DocsTry Aspire
DocsTry

Connect to Mailpit

⭐ Community Toolkit Mailpit logo

This page describes how consuming apps connect to a Mailpit resource that’s already modeled in your AppHost. For the AppHost API surface — adding a Mailpit resource, the SMTP endpoint, the HTTP UI endpoint, and data volumes — see Mailpit Hosting integration.

When you reference a Mailpit resource from your AppHost, Aspire injects an SMTP connection string and individual connection properties into the consuming app. There isn’t a dedicated Aspire client package for Mailpit, so each app passes that information to a stable SMTP library directly.

Aspire injects the resource connection string as ConnectionStrings__mailpit. It also exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Host property of a resource called mailpit becomes MAILPIT_HOST.

The Mailpit resource exposes the following connection properties:

Property NameDescription
HostThe hostname or IP address of the Mailpit SMTP server
PortThe port number the Mailpit SMTP server is listening on (default: 1025)
UriThe SMTP endpoint URI, with the format smtp://{Host}:{Port}

Example environment variable values for a resource named mailpit:

ConnectionStrings__mailpit=Endpoint=smtp://localhost:1025
MAILPIT_HOST=localhost
MAILPIT_PORT=1025
MAILPIT_URI=smtp://localhost:1025

The Mailpit web UI is a separate http endpoint. Open it from the resource’s endpoint list in the Aspire dashboard.

Pick the language your consuming app is written in. Each example assumes your AppHost adds a Mailpit resource named mailpit and references it from the consuming app.

The toolkit’s tested C# pattern reads the Aspire connection string and passes its Endpoint value to System.Net.Mail.SmtpClient.

Program.cs
using System.Data.Common;
using System.Net.Mail;
var connectionString =
builder.Configuration.GetConnectionString("mailpit")
?? throw new InvalidOperationException("Mailpit connection string is missing.");
var connectionBuilder = new DbConnectionStringBuilder
{
ConnectionString = connectionString,
};
var smtpEndpoint = new Uri(
(string)connectionBuilder["Endpoint"],
UriKind.Absolute);
Program.cs
using var client = new SmtpClient(smtpEndpoint.Host, smtpEndpoint.Port);
using var message = new MailMessage(
from: "sender@example.com",
to: "recipient@example.com",
subject: "Hello from Aspire",
body: "This email was captured by Mailpit.");
await client.SendMailAsync(message);

Register an SMTP client factory when multiple services need to send email:

Program.cs
builder.Services.AddTransient(_ =>
new SmtpClient(smtpEndpoint.Host, smtpEndpoint.Port));