https://github.com/cristipufu/aspire-hosting-mailcatcher
MailCatcher support for .NET Aspire
https://github.com/cristipufu/aspire-hosting-mailcatcher
aspire mail mailcatcher maildev net8 smtp
Last synced: 8 months ago
JSON representation
MailCatcher support for .NET Aspire
- Host: GitHub
- URL: https://github.com/cristipufu/aspire-hosting-mailcatcher
- Owner: cristipufu
- License: mit
- Created: 2024-06-30T14:52:11.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-07-01T11:31:03.000Z (over 1 year ago)
- Last Synced: 2025-01-14T00:32:16.235Z (9 months ago)
- Topics: aspire, mail, mailcatcher, maildev, net8, smtp
- Language: C#
- Homepage:
- Size: 20.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MailCatcher.Hosting library
[](https://www.nuget.org/packages/MailCatcher.Hosting)
[](https://github.com/cristipufu/aspire-hosting-mailcatcher/blob/master/LICENSE)Provides extension methods and resource definitions for a .NET Aspire AppHost to configure a MailCatcher resource.
## Getting started
### Install the package
In your AppHost project, install the .NET Aspire MailCatcher Hosting library with NuGet:
```dotnetcli
dotnet add package MailCatcher.Hosting
```## Usage example
Then, in the _Program.cs_ file of `AppHost`, add a MailCatcher resource and consume the connection using the following methods:
```csharp
var mail = builder.AddMailCatcher("mailcatcher");var api = builder.AddProject("api")
.WithReference(mail);
```In the _Program.cs_ file of the `WebApi`, configure the `SmtpClient`:
```charp
builder.Services.AddSingleton(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);
});
``````http
POST /subscribe?email=test@test.com HTTP/1.1
Host: localhost:7251
Content-Type: application/json
```
## Feedback & contributingContributions are welcome! Whether you're fixing a bug, adding a new feature, or improving the documentation, please feel free to make a pull request.