Provides extension methods and resource definitions for a .NET Aspire AppHost to configure a MailCatcher resource.
In your AppHost project, install the .NET Aspire MailCatcher Hosting library with NuGet:
dotnet add package MailCatcher.Hosting
Then, in the Program.cs file of AppHost
, add a MailCatcher resource and consume the connection using the following methods:
var mail = builder.AddMailCatcher("mailcatcher");
var api = builder.AddProject<Projects.MailCatcher_DemoApi>("api")
.WithReference(mail);
In the Program.cs file of the WebApi
, configure the SmtpClient
:
builder.Services.AddSingleton<SmtpClient>(sp =>
{
var smtpUri = new Uri(builder.Configuration.GetConnectionString("mailcatcher")!);
var smtpClient = new SmtpClient(smtpUri.Host, smtpUri.Port);
return smtpClient;
});
app.MapPost("/subscribe", async ([FromServices] SmtpClient smtpClient, string email) =>
{
using var message = new MailMessage("hi@company.com", email)
{
Subject = "Welcome to our app!",
Body = "Thank you for subscribing!"
};
await smtpClient.SendMailAsync(message);
});
POST /subscribe?email=test@test.com HTTP/1.1
Host: localhost:7251
Content-Type: application/json
Contributions are welcome! Whether you're fixing a bug, adding a new feature, or improving the documentation, please feel free to make a pull request.