コンテンツにスキップ
Docs Try Aspire
Docs Try

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 the SMTP connection information into the consuming app as environment variables. Your app reads those variables and passes them to any standard SMTP library — the pattern works the same from any language.

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the SmtpHost property of a resource called mailpit becomes MAILPIT_SMTPHOST.

The Mailpit resource exposes the following connection properties:

Property NameDescription
SmtpHostThe hostname or IP address of the Mailpit SMTP server
SmtpPortThe port number the Mailpit SMTP server is listening on (default: 1025)
HttpEndpointThe base URL of the Mailpit web UI (default: http://{host}:8025)

Example environment variable values for a resource named mailpit:

MAILPIT_SMTPHOST=localhost
MAILPIT_SMTPPORT=1025
MAILPIT_HTTPENDPOINT=http://localhost:8025

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.

There is no dedicated Aspire client integration package for Mailpit. Instead, read the Aspire-injected environment variables and use System.Net.Mail.SmtpClient (or any other SMTP library) to send emails.

C# — Program.cs
using System.Net.Mail;
// Read Aspire-injected SMTP connection properties
var smtpHost = Environment.GetEnvironmentVariable("MAILPIT_SMTPHOST") ?? "localhost";
var smtpPort = int.TryParse(
Environment.GetEnvironmentVariable("MAILPIT_SMTPPORT"), out var p) ? p : 1025;
C# — Program.cs
using var client = new SmtpClient(smtpHost, smtpPort);
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);

Open the Mailpit web UI via the HttpEndpoint property:

C# — Program.cs
var mailpitUiUrl = Environment.GetEnvironmentVariable("MAILPIT_HTTPENDPOINT");
// Navigate to mailpitUiUrl in a browser to inspect captured emails.

For production-style registration, wrap the SMTP configuration in a hosted service or factory:

C# — Program.cs
builder.Services.AddSingleton<SmtpClient>(_ =>
new SmtpClient(
host: Environment.GetEnvironmentVariable("MAILPIT_SMTPHOST") ?? "localhost",
port: int.TryParse(
Environment.GetEnvironmentVariable("MAILPIT_SMTPPORT"), out var p) ? p : 1025));