Skip to content

Commit

Permalink
#240 add papercut smtp hosting (#423)
Browse files Browse the repository at this point in the history
* #240 add papercut smtp hosting

* #240 Deleted api tag from papercut

* #240 set endpointname for papercut unittest

* #240 try to implement health

* #240 changed unittest on papercut to call papercut directly

* #240 changed README.md's

* #240 revert changes in non-papercut places

* #240 revert non-papercut changes

* #240: update Papercut SMTP integration and tests

* #240 changed README.md to reflect the new connectionstring convention

---------

Co-authored-by: Aaron Powell <[email protected]>
  • Loading branch information
anoordover and aaronpowell authored Feb 3, 2025
1 parent 0c1260d commit 776688a
Show file tree
Hide file tree
Showing 21 changed files with 572 additions and 0 deletions.
38 changes: 38 additions & 0 deletions CommunityToolkit.Aspire.sln
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Mic
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite.Tests", "tests\CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite.Tests\CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite.Tests.csproj", "{52846E18-99D1-4040-AF5F-17FC69198BCE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.PapercutSmtp", "src\CommunityToolkit.Aspire.Hosting.PapercutSmtp\CommunityToolkit.Aspire.Hosting.PapercutSmtp.csproj", "{E267907F-4467-4504-9947-2A5A6940DE9B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "papercut", "papercut", "{2B0503E5-4FD5-4ED5-8AFA-8FC9609FCEC1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.PapercutSmtp.AppHost", "examples\papercut\CommunityToolkit.Aspire.Hosting.PapercutSmtp.AppHost\CommunityToolkit.Aspire.Hosting.PapercutSmtp.AppHost.csproj", "{9772004D-0905-421A-98C6-DAE1C35080B3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.PapercutSmtp.ServiceDefaults", "examples\papercut\CommunityToolkit.Aspire.Hosting.PapercutSmtp.ServiceDefaults\CommunityToolkit.Aspire.Hosting.PapercutSmtp.ServiceDefaults.csproj", "{449B3661-A6F2-46D3-8EC9-B515362A72C1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi", "examples\papercut\CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi\CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi.csproj", "{70A77931-7D25-4CBF-AE55-F5A3B932E9F8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.PapercutSmtp.Tests", "tests\CommunityToolkit.Aspire.Hosting.PapercutSmtp.Tests\CommunityToolkit.Aspire.Hosting.PapercutSmtp.Tests.csproj", "{80CCC017-3821-4F7D-902C-BB71DE875F58}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.Dapr", "src\CommunityToolkit.Aspire.Hosting.Dapr\CommunityToolkit.Aspire.Hosting.Dapr.csproj", "{2165F65B-83F2-4269-8781-86AB6ACF043D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.Dapr.Tests", "tests\CommunityToolkit.Aspire.Hosting.Dapr.Tests\CommunityToolkit.Aspire.Hosting.Dapr.Tests.csproj", "{B2384D1A-DD13-4D03-B8FE-B194DEF71A0C}"
Expand Down Expand Up @@ -643,6 +655,26 @@ Global
{52846E18-99D1-4040-AF5F-17FC69198BCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52846E18-99D1-4040-AF5F-17FC69198BCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52846E18-99D1-4040-AF5F-17FC69198BCE}.Release|Any CPU.Build.0 = Release|Any CPU
{E267907F-4467-4504-9947-2A5A6940DE9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E267907F-4467-4504-9947-2A5A6940DE9B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E267907F-4467-4504-9947-2A5A6940DE9B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E267907F-4467-4504-9947-2A5A6940DE9B}.Release|Any CPU.Build.0 = Release|Any CPU
{9772004D-0905-421A-98C6-DAE1C35080B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9772004D-0905-421A-98C6-DAE1C35080B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9772004D-0905-421A-98C6-DAE1C35080B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9772004D-0905-421A-98C6-DAE1C35080B3}.Release|Any CPU.Build.0 = Release|Any CPU
{449B3661-A6F2-46D3-8EC9-B515362A72C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{449B3661-A6F2-46D3-8EC9-B515362A72C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{449B3661-A6F2-46D3-8EC9-B515362A72C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{449B3661-A6F2-46D3-8EC9-B515362A72C1}.Release|Any CPU.Build.0 = Release|Any CPU
{70A77931-7D25-4CBF-AE55-F5A3B932E9F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70A77931-7D25-4CBF-AE55-F5A3B932E9F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70A77931-7D25-4CBF-AE55-F5A3B932E9F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70A77931-7D25-4CBF-AE55-F5A3B932E9F8}.Release|Any CPU.Build.0 = Release|Any CPU
{80CCC017-3821-4F7D-902C-BB71DE875F58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{80CCC017-3821-4F7D-902C-BB71DE875F58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{80CCC017-3821-4F7D-902C-BB71DE875F58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{80CCC017-3821-4F7D-902C-BB71DE875F58}.Release|Any CPU.Build.0 = Release|Any CPU
{2165F65B-83F2-4269-8781-86AB6ACF043D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2165F65B-83F2-4269-8781-86AB6ACF043D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2165F65B-83F2-4269-8781-86AB6ACF043D}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -816,6 +848,12 @@ Global
{0E6EBCFB-DEF5-496C-95AF-00884826CFC8} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
{861FE61C-90EE-49B0-BCC8-8417C293CC21} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
{52846E18-99D1-4040-AF5F-17FC69198BCE} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
{E267907F-4467-4504-9947-2A5A6940DE9B} = {414151D4-7009-4E78-A5C6-D99EBD1E67D1}
{2B0503E5-4FD5-4ED5-8AFA-8FC9609FCEC1} = {8519CC01-1370-47C8-AD94-B0F326B1563F}
{9772004D-0905-421A-98C6-DAE1C35080B3} = {2B0503E5-4FD5-4ED5-8AFA-8FC9609FCEC1}
{449B3661-A6F2-46D3-8EC9-B515362A72C1} = {2B0503E5-4FD5-4ED5-8AFA-8FC9609FCEC1}
{70A77931-7D25-4CBF-AE55-F5A3B932E9F8} = {2B0503E5-4FD5-4ED5-8AFA-8FC9609FCEC1}
{80CCC017-3821-4F7D-902C-BB71DE875F58} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
{2165F65B-83F2-4269-8781-86AB6ACF043D} = {414151D4-7009-4E78-A5C6-D99EBD1E67D1}
{B2384D1A-DD13-4D03-B8FE-B194DEF71A0C} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
{E3C2B4B7-B3B0-4E7F-A975-A6C7FD926792} = {8519CC01-1370-47C8-AD94-B0F326B1563F}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<Sdk Name="Aspire.AppHost.Sdk" Version="$(AspireAppHostSdkVersion)" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<UserSecretsId>f60c6ce9-5628-467c-a6fc-2fc7938b38ad</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting" />
<PackageReference Include="Aspire.Hosting.AppHost" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Hosting.PapercutSmtp\CommunityToolkit.Aspire.Hosting.PapercutSmtp.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi\CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Aspire.Hosting;
using Projects;

var builder = DistributedApplication.CreateBuilder(args);

var papercut = builder.AddPapercutSmtp("papercut");

var sendmail = builder.AddProject<CommunityToolkit_Aspire_Hosting_PapercutSmtp_SendMailApi>("sendmail")
.WithReference(papercut)
.WaitFor(papercut);

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CommunityToolkit.Aspire.Hosting.PapercutSmtp.ServiceDefaults\CommunityToolkit.Aspire.Hosting.PapercutSmtp.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi_HostAddress = http://localhost:5075

POST http://localhost:5075/send
Accept: application/json
Content-Type: application/json

{
"From": "[email protected]",
"To": "[email protected]",
"Body": "Hello World",
"Subject": "Test"
}
###
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;

namespace CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi;

public class MailData
{
[Required]
public required string From { get; set; }
[Required]
public required string To { get; set; }
[Required]
public required string Body { get; set; }
[Required]
public required string Subject { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using CommunityToolkit.Aspire.Hosting.PapercutSmtp.SendMailApi;
using Microsoft.AspNetCore.Mvc;
using System.Data.Common;
using System.Net.Mail;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);

string? papercutConnectionString = builder.Configuration.GetConnectionString("papercut");
DbConnectionStringBuilder connectionBuilder = new()
{
ConnectionString = papercutConnectionString
};

Uri endpoint = new(connectionBuilder["Endpoint"].ToString()!, UriKind.Absolute);
builder.Services.AddScoped(_ => new SmtpClient(endpoint.Host, endpoint.Port));
builder.AddServiceDefaults();
WebApplication app = builder.Build();

app.MapPost("/send", ([FromBody]MailData mailData, [FromServices] SmtpClient smtpClient) =>
{
MailMessage myMail = CreateMailMessage(mailData);
smtpClient.Send(myMail);
})
.WithName("SendMail");

app.MapGet("/health", () => "OK");

app.MapDefaultEndpoints();
app.Run();
return;

MailMessage CreateMailMessage(MailData mailData)
{
MailAddress from = new(mailData.From, "TestFromName");
MailAddress to = new(mailData.To, "TestToName");
MailMessage mailMessage = new(from, to);
MailAddress replyTo = new(mailData.From);
mailMessage.ReplyToList.Add(replyTo);
mailMessage.Subject = mailData.Subject;
mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;
mailMessage.Body = mailData.Body;
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
return mailMessage;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Extensions.Http.Resilience" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.Hosting;

// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
// This project should be referenced by each service project in your solution.
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
public static class Extensions
{
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.ConfigureOpenTelemetry();

builder.AddDefaultHealthChecks();

builder.Services.AddServiceDiscovery();

builder.Services.ConfigureHttpClientDefaults(http =>
{
// Turn on resilience by default
http.AddStandardResilienceHandler();

// Turn on service discovery by default
http.AddServiceDiscovery();
});

// Uncomment the following to restrict the allowed schemes for service discovery.
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
// {
// options.AllowedSchemes = ["https"];
// });

return builder;
}

public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Logging.AddOpenTelemetry(logging =>
{
logging.IncludeFormattedMessage = true;
logging.IncludeScopes = true;
});

builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation();
})
.WithTracing(tracing =>
{
tracing.AddSource(builder.Environment.ApplicationName)
.AddAspNetCoreInstrumentation()
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
//.AddGrpcClientInstrumentation()
.AddHttpClientInstrumentation();
});

builder.AddOpenTelemetryExporters();

return builder;
}

private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);

if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}

// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
//{
// builder.Services.AddOpenTelemetry()
// .UseAzureMonitor();
//}

return builder;
}

public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
{
builder.Services.AddHealthChecks()
// Add a default liveness check to ensure app is responsive
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);

return builder;
}

public static WebApplication MapDefaultEndpoints(this WebApplication app)
{
// Adding health checks endpoints to applications in non-development environments has security implications.
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
if (app.Environment.IsDevelopment())
{
// All health checks must pass for app to be considered ready to accept traffic after starting
app.MapHealthChecks("/health");

// Only health checks tagged with the "live" tag must pass for app to be considered alive
app.MapHealthChecks("/alive", new HealthCheckOptions
{
Predicate = r => r.Tags.Contains("live")
});
}

return app;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>An Aspire component leveraging Papercut SMTP container.</Description>
<AdditionalPackageTags>papercut smtp hosting</AdditionalPackageTags>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="CommunityToolkit.Aspire.Hosting.PapercutSmtp.Tests" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace CommunityToolkit.Aspire.Hosting.PapercutSmtp;

internal static class PapercutSmtpContainerImageTags
{
public const string Registry = "docker.io";
public const string Image = "changemakerstudiosus/papercut-smtp";
public const string Tag = "7.0.0-rc1";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// Resource for the Papercut SMTP server.
/// </summary>
/// <param name="name"></param>
public class PapercutSmtpContainerResource(string name) : ContainerResource(name), IResourceWithConnectionString
{
internal const int HttpEndpointPort = 80;
internal const int SmtpEndpointPort = 25;
internal const string HttpEndpointName = "http";
internal const string SmtpEndpointName = "smtp";
private EndpointReference? _smtpEndpoint;
private EndpointReference SmtpEndpoint => _smtpEndpoint ??= new EndpointReference(this, SmtpEndpointName);

/// <summary>
/// ConnectionString for the Papercut SMTP server in the form of smtp://host:port.
/// </summary>
public ReferenceExpression ConnectionStringExpression => ReferenceExpression.Create(
$"Endpoint={SmtpEndpoint.Scheme}://{SmtpEndpoint.Property(EndpointProperty.Host)}:{SmtpEndpoint.Property(EndpointProperty.Port)}");
}
Loading

0 comments on commit 776688a

Please sign in to comment.