-
-
Notifications
You must be signed in to change notification settings - Fork 356
feat: Add Mailpit module #1727
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HofmeisterAn
merged 8 commits into
testcontainers:develop
from
0xced:feature/add-mailpit
Aug 12, 2026
Merged
feat: Add Mailpit module #1727
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cc70a2f
feat: mailpit module configuration
feslima 2405430
Refine the Mailpit module API and documentation
0xced 6c5f8ac
Merge remote-tracking branch 'origin/develop' into fork/0xced/feature…
HofmeisterAn 25c6a78
chore: Apply some module standards
HofmeisterAn 311e325
chore: Align tests
HofmeisterAn 0802d62
chore: Rename HTTP client
HofmeisterAn 9e930ec
chore: Refactor Mailpit builder configuration handling
HofmeisterAn e1df8a4
chore: Disconnect from SMTP server
HofmeisterAn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| root = true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| namespace Testcontainers.Mailpit; | ||
|
|
||
| /// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
| [PublicAPI] | ||
| public sealed class MailpitBuilder : ContainerBuilder<MailpitBuilder, MailpitContainer, MailpitConfiguration> | ||
| { | ||
| [Obsolete("This constant is obsolete and will be removed in the future. Use the constructor with the image parameter instead: https://github.com/testcontainers/testcontainers-dotnet/discussions/1470#discussioncomment-15185721.")] | ||
| public const string MailpitImage = "axllent/mailpit:v1.30"; | ||
|
|
||
| public const ushort SmtpPort = 1025; | ||
|
|
||
| public const ushort WebPort = 8025; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MailpitBuilder" /> class. | ||
| /// </summary> | ||
| [Obsolete("This parameterless constructor is obsolete and will be removed. Use the constructor with the image parameter instead: https://github.com/testcontainers/testcontainers-dotnet/discussions/1470#discussioncomment-15185721.")] | ||
| public MailpitBuilder() | ||
| :this(MailpitImage) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MailpitBuilder" /> class. | ||
| /// </summary> | ||
| /// <param name="image"> | ||
| /// The full Docker image name, including the image repository and tag | ||
| /// (e.g., <c>axllent/mailpit:v1.30</c>). | ||
| /// </param> | ||
| /// <remarks> | ||
| /// Docker image tags available at <see href="https://hub.docker.com/r/axllent/mailpit/tags" />. | ||
| /// </remarks> | ||
| public MailpitBuilder(string image) | ||
| : this(new DockerImage(image)) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MailpitBuilder" /> class. | ||
| /// </summary> | ||
| /// <param name="image"> | ||
| /// An <see cref="IImage" /> instance that specifies the Docker image to be used | ||
| /// for the container builder configuration. | ||
| /// </param> | ||
| /// <remarks> | ||
| /// Docker image tags available at <see href="https://hub.docker.com/r/axllent/mailpit/tags" />. | ||
| /// </remarks> | ||
| public MailpitBuilder(IImage image) | ||
| : this(new MailpitConfiguration()) | ||
| { | ||
| DockerResourceConfiguration = Init().WithImage(image).DockerResourceConfiguration; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MailpitBuilder" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| private MailpitBuilder(MailpitConfiguration resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| DockerResourceConfiguration = resourceConfiguration; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override MailpitConfiguration DockerResourceConfiguration { get; } | ||
|
|
||
| /// <summary> | ||
| /// Sets the Mailpit <c>MP_SMTP_AUTH</c> configuration. | ||
| /// </summary> | ||
| /// <param name="credentials">The credentials to use for SMTP authentication.</param> | ||
| /// <param name="allowInsecure"> | ||
| /// When <see langword="true" />, sets the Mailpit <c>MP_SMTP_AUTH_ALLOW_INSECURE</c> | ||
| /// configuration to allow insecure PLAIN and LOGIN SMTP authentication. | ||
| /// When <see langword="false" />, configures Mailpit to use a self-signed | ||
| /// certificate by setting <c>MP_SMTP_TLS_CERT</c> and <c>MP_SMTP_TLS_KEY</c> | ||
| /// to <c>sans:localhost</c>. The certificate subject and issuer are | ||
| /// <c>CN=localhost, O=Mailpit self-signed certificate</c>. | ||
| /// </param> | ||
| /// <remarks> | ||
| /// See <see href="https://mailpit.axllent.org/docs/configuration/certificates/#auto-generate-self-signed-certificates" />. | ||
| /// </remarks> | ||
| /// <returns>A configured <see cref="MailpitBuilder" /> instance.</returns> | ||
| public MailpitBuilder WithSmtpAuthCredentials(NetworkCredential credentials, bool allowInsecure) | ||
| { | ||
| if (credentials == null) | ||
| { | ||
| throw new ArgumentNullException(nameof(credentials)); | ||
| } | ||
|
|
||
| if (credentials.UserName.Contains(":")) | ||
| { | ||
| throw new ArgumentException("The UserName cannot contain a colon (:) character.", nameof(credentials)); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| // TODO: | ||
| // This prepares a full environment replacement once dictionary composition | ||
| // supports ComposableDictionary for environments. Until that, stale keys | ||
| // are still preserved by the current IReadOnlyDictionary merge behavior. | ||
| var environments = DockerResourceConfiguration.Environments.ToDictionary(item => item.Key, item => item.Value); | ||
| environments.Remove("MP_SMTP_AUTH_ALLOW_INSECURE"); | ||
| environments.Remove("MP_SMTP_TLS_CERT"); | ||
| environments.Remove("MP_SMTP_TLS_KEY"); | ||
|
|
||
| // https://mailpit.axllent.org/docs/configuration/smtp/#adding-smtp-authentication. | ||
| environments["MP_SMTP_AUTH"] = $"{credentials.UserName}:{credentials.Password}"; | ||
|
|
||
| if (allowInsecure) | ||
| { | ||
| environments["MP_SMTP_AUTH_ALLOW_INSECURE"] = "1"; | ||
| } | ||
| else | ||
| { | ||
| environments["MP_SMTP_TLS_CERT"] = "sans:localhost"; | ||
| environments["MP_SMTP_TLS_KEY"] = "sans:localhost"; | ||
| } | ||
|
|
||
| return WithEnvironment(new OverwriteDictionary<string, string>(environments)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the Mailpit <c>MP_MAX_MESSAGES</c> configuration. | ||
| /// Specifies the maximum number of messages to store. Mailpit periodically | ||
| /// deletes the oldest messages when this limit is exceeded. | ||
| /// Set to <c>0</c> to disable automatic deletion. | ||
| /// </summary> | ||
| /// <param name="maxMessages">The maximum number of messages to store.</param> | ||
| /// <returns>A configured <see cref="MailpitBuilder" /> instance.</returns> | ||
| public MailpitBuilder WithMaxMessages(uint maxMessages) | ||
| { | ||
| return WithEnvironment("MP_MAX_MESSAGES", maxMessages.ToString()); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override MailpitContainer Build() | ||
| { | ||
| Validate(); | ||
| return new MailpitContainer(DockerResourceConfiguration); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override MailpitBuilder Init() | ||
| { | ||
| return base.Init() | ||
| .WithPortBinding(SmtpPort, true) | ||
| .WithPortBinding(WebPort, true) | ||
| .WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request => | ||
| request.ForPath("/readyz").ForPort(WebPort))); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override MailpitBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new MailpitConfiguration(resourceConfiguration)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override MailpitBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new MailpitConfiguration(resourceConfiguration)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override MailpitBuilder Merge(MailpitConfiguration oldValue, MailpitConfiguration newValue) | ||
| { | ||
| return new MailpitBuilder(new MailpitConfiguration(oldValue, newValue)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| namespace Testcontainers.Mailpit; | ||
|
|
||
| /// <inheritdoc cref="ContainerConfiguration" /> | ||
| [PublicAPI] | ||
| public sealed class MailpitConfiguration : ContainerConfiguration | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MailpitConfiguration" /> class. | ||
| /// </summary> | ||
| public MailpitConfiguration() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MailpitConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public MailpitConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MailpitConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public MailpitConfiguration(IContainerConfiguration resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MailpitConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public MailpitConfiguration(MailpitConfiguration resourceConfiguration) | ||
| : this(new MailpitConfiguration(), resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MailpitConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="oldValue">The old Docker resource configuration.</param> | ||
| /// <param name="newValue">The new Docker resource configuration.</param> | ||
| public MailpitConfiguration(MailpitConfiguration oldValue, MailpitConfiguration newValue) | ||
| : base(oldValue, newValue) | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| namespace Testcontainers.Mailpit; | ||
|
|
||
| /// <inheritdoc cref="DockerContainer" /> | ||
| [PublicAPI] | ||
| public sealed class MailpitContainer : DockerContainer | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MailpitContainer" /> class. | ||
| /// </summary> | ||
| /// <param name="configuration">The container configuration.</param> | ||
| public MailpitContainer(MailpitConfiguration configuration) | ||
| : base(configuration) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the SMTP port. | ||
| /// </summary> | ||
| public ushort SmtpPort => GetMappedPublicPort(MailpitBuilder.SmtpPort); | ||
|
|
||
| /// <summary> | ||
| /// Gets the Mailpit web server address. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// This address can also be used as the base URL for the <see href="https://mailpit.axllent.org/docs/api-v1/">REST API</see>. | ||
| /// </remarks> | ||
| /// <returns>The Mailpit web server address.</returns> | ||
| public string GetWebAddress() | ||
| { | ||
| return new UriBuilder(Uri.UriSchemeHttp, Hostname, GetMappedPublicPort(MailpitBuilder.WebPort)).ToString(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>net8.0;net9.0;net10.0;netstandard2.0;netstandard2.1</TargetFrameworks> | ||
| <LangVersion>latest</LangVersion> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="../Testcontainers/Testcontainers.csproj"/> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| global using System; | ||
| global using System.Linq; | ||
| global using System.Net; | ||
| global using Docker.DotNet.Models; | ||
| global using DotNet.Testcontainers.Builders; | ||
| global using DotNet.Testcontainers.Configurations; | ||
| global using DotNet.Testcontainers.Containers; | ||
| global using DotNet.Testcontainers.Images; | ||
| global using JetBrains.Annotations; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| root = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ubuntu-24.04 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| FROM axllent/mailpit:v1.30@sha256:5a49a77c5bdbe7c5474450b4f46348d09949df3695257729c93a30369382d4f6 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.