Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions docs/community-toolkit/hosting-mailpit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
title: .NET Aspire MailPit hosting integration
description: Learn how to use the .NET Aspire MailPit hosting integration to add MailPit containers to your distributed application.
ms.date: 08/22/2025
ai-usage: ai-generated
---

# .NET Aspire MailPit hosting integration

In this article, you learn how to use the .NET Aspire MailPit hosting integration. The `CommunityToolkit.Aspire.Hosting.MailPit` library is used to register [MailPit](https://mailpit.axllent.org/) containers in your distributed application.

MailPit is a mail testing tool that provides a fake SMTP server for development and testing. It provides a web interface where you can view and manage captured emails, making it ideal for testing email functionality in your applications.

## Hosting integration

The MailPit hosting integration models MailPit as the `MailPitResource` type. To access this resource and the extension methods to add it to your app model, add the [📦 CommunityToolkit.Aspire.Hosting.MailPit](https://nuget.org/packages/CommunityToolkit.Aspire.Hosting.MailPit) NuGet package in the [app host](../fundamentals/app-host-overview.md) project.

### [.NET CLI](#tab/dotnet-cli)

```dotnetcli
dotnet add package CommunityToolkit.Aspire.Hosting.MailPit
```

### [PackageReference](#tab/package-reference)

```xml
<PackageReference Include="CommunityToolkit.Aspire.Hosting.MailPit"
Version="*" />
```

---

In your app host project, register a MailPit resource using the `AddMailPit` extension method:

```csharp
var builder = DistributedApplication.CreateBuilder(args);

var mailpit = builder.AddMailPit("mailpit");

builder.AddProject<Projects.ExampleProject>()
.WithReference(mailpit);

builder.Build().Run();
```

The `AddMailPit` method adds a MailPit resource to the application model. The resource is configured to use the default MailPit image and ports.

When you add a MailPit resource, the following services are exposed:

| Service | Scheme | Port |
|---------|--------|------|
| SMTP | `smtp` | 1025 |
| Web UI | `http` | 8025 |

The SMTP service can be used by your applications to send emails for testing purposes, while the Web UI provides an interface to view captured emails.

## Data persistence

By default, MailPit stores emails in memory, which means all emails are lost when the container is stopped. To persist emails across container restarts, you can add a data volume:

```csharp
var mailpit = builder.AddMailPit("mailpit")
.WithDataVolume("mailpit-data");
```

The data volume persists the MailPit data directory, ensuring emails are retained across container restarts.

You can also use a bind mount to map a local directory to the MailPit container:

```csharp
var mailpit = builder.AddMailPit("mailpit")
.WithDataBindMount(@"C:\MailPitData");
```

## Client integration

There's no Community Toolkit client integration for MailPit. Instead, use any SMTP client library with the connection string provided by the MailPit resource. The MailPit resource provides an SMTP connection string that can be used to send emails to the MailPit server.

The connection string includes the following information:

- **Host**: The hostname or IP address of the MailPit SMTP server
- **Port**: The port number (typically 1025)

Here's an example of how to use the connection string in a .NET application:

```csharp
public class EmailService
{
private readonly IConfiguration _configuration;

public EmailService(IConfiguration configuration)
{
_configuration = configuration;
}

public async Task SendEmailAsync(string to, string subject, string body)
{
var connectionString = _configuration.GetConnectionString("mailpit");
var connectionInfo = ConnectionString.Parse(connectionString);

using var client = new SmtpClient();
await client.ConnectAsync(connectionInfo.Host, connectionInfo.Port, SecureSocketOptions.None);

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Test Sender", "[email protected]"));
message.To.Add(new MailboxAddress("", to));
message.Subject = subject;
message.Body = new TextPart("plain") { Text = body };

await client.SendAsync(message);
await client.DisconnectAsync(true);
}
}
```

This example uses the [MailKit](https://nuget.org/packages/MailKit) library to send emails via the MailPit SMTP server.

## See also

- [MailPit documentation](https://mailpit.axllent.org/)
- [.NET Aspire integrations overview](../fundamentals/integrations-overview.md)
- [.NET Aspire Community Toolkit overview](overview.md)
5 changes: 4 additions & 1 deletion docs/community-toolkit/overview.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Overview
description: An overview of the .NET Aspire Community Toolkit project.
ms.date: 11/05/2024
ms.date: 01/22/2025
---

# .NET Aspire Community Toolkit
Expand Down Expand Up @@ -39,6 +39,9 @@ The community toolkit is a growing project, publishing a set of NuGet packages.
- The [Meilisearch](https://www.meilisearch.com) integration enables hosting Meilisearch containers.
- [📄 .NET Aspire Meilisearch hosting integration](hosting-meilisearch.md#hosting-integration).
- [📦 CommunityToolkit.Aspire.Hosting.Meilisearch](https://nuget.org/packages/CommunityToolkit.Aspire.Hosting.Meilisearch).
- The [MailPit](https://mailpit.axllent.org/) integration provides a fake SMTP server for mail testing during development:
- [📄 .NET Aspire MailPit hosting integration](hosting-mailpit.md).
- [📦 CommunityToolkit.Aspire.Hosting.MailPit](https://nuget.org/packages/CommunityToolkit.Aspire.Hosting.MailPit).
- The [Rust apps](https://www.rust-lang.org/) integration provides support for hosting Rust applications.
- [📄 .NET Aspire Rust hosting integration](hosting-rust.md#hosting-integration).
- [📦 CommunityToolkit.Aspire.Hosting.Rust](https://nuget.org/packages/CommunityToolkit.Aspire.Hosting.Rust).
Expand Down
2 changes: 2 additions & 0 deletions docs/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,8 @@ items:
href: community-toolkit/ollama.md
- name: Meilisearch
href: community-toolkit/hosting-meilisearch.md
- name: MailPit
href: community-toolkit/hosting-mailpit.md
- name: Rust apps
href: community-toolkit/hosting-rust.md
- name: SQL Database Projects
Expand Down