Skip to content

Add mail sender support for spring-boot-testcontainers #35127

Open

Description

@linux-china

Mail catcher is good solution to test email sending by SMTP, such as:

It will be good to add MailConnectionDetails support to make email sending test easy with mail catcher container.

Activity

  1. changed the title [-]Mail sender support for spring-boot-testcontainers[/-] [+]Add mail sender support for spring-boot-testcontainers[/+] on Apr 25, 2023
  2. added and removed
    status: waiting-for-triageAn issue that has not yet been triaged. Contributions will not be accepted.
    on Apr 25, 2023
  3. added this to the 3.1.x milestone on Apr 25, 2023
  4. wilkinsona commented on Apr 28, 2023

    @wilkinsona
    Member

    We could update the auto-configuration to add MailConnectionDetails but I'm not sure how much we can do beyond that. As the list above shows, there are a variety of options for running an SMTP server. In terms of official Docker images for the above, there are:

    How the SMTP server is configured differs from image to image so I don't think we can offer a general purpose implementation for getting connection details from a container. MailHog appears to be the most popular, so perhaps that's the place to start? It would feel a little odd to have MailConnectionDetails and only have a properties-based implementation.

  5. linux-china commented on Apr 28, 2023

    @linux-china
    Author

    MailHog is good for me. All these mail catchers, they almost use 1025 as SMTP port, and I think we just use 1025 as exposed port for spring.mail.port.

  6. wilkinsona commented on Apr 28, 2023

    @wilkinsona
    Member

    The port should be fine. It's username and password that are more complicated. For example, maildev uses MAILDEV_OUTGOING_USER and MAILDEV_OUTGOING_PASS environment variables to configure the credentials for sending mail. These are easy for us to extract from the container and use in the connection details. On the other hand, MailHog seems to use a file which would be much harder to deal with unless we only supported the defaults.

  7. linux-china commented on Apr 29, 2023

    @linux-china
    Author

    @wilkinsona, is it possible to introduce org.springframework.boot.properties.xxxx labels, and these labels will be added ad Spring Boot properties. Example as following:

    services:
      mail:
        image: "maildev/maildev"
        ports:
          - '1025'
        labels:
          org.springframework.boot.properties.spring.mail.host = "${container.getHost()}"
          org.springframework.boot.properties.spring.mail.port = "${container.getMappedPort(1025)}"
          org.springframework.boot.properties.spring.mail.username = demo

    Value cloud be a SpEL, and container is the variable of GenericContainer or sub GenericContainer.
    This way is still ok for other containers, such as NATS, Apache Pulsar etc.

  8. eddumelendez commented on Apr 29, 2023

    @eddumelendez
    Contributor

    It would feel a little odd to have MailConnectionDetails and only have a properties-based implementation.

    Given ConnectionDetails is the entrypoint to do this, it wouldn't be that odd. Doing that, will allow to users create libraries with specific ConnectionDetailsFactory where there are more than one image or not an official image to choose such as mail and activemq. One example is RedisContainerConnectionsDetails, currently only supports standalone but there are images where cluster can be enabled and it's totally fine if spring boot doesn't provide RedisClusterConnectionDetailsFactory but the infrastructure is already there to do it for third-parties.

    This is probably related.

  9. eddumelendez commented on May 3, 2023

    @eddumelendez
    Contributor

    For cases like this one and probably #35080 26566d4 would fix it

  10. modified the milestones: 3.1.x, 3.x on May 3, 2023
  11. dominik-kovacs commented on Mar 24, 2024

    @dominik-kovacs

    this would be a wonderful feature, however, I am not sure how much is MailHog maintained

    edit:
    https://github.com/axllent/mailpit This seems to be an actively maintained project

  12. eddumelendez commented on Sep 25, 2024

    @eddumelendez
    Contributor

    I learn about mailpit today and seems good https://hub.docker.com/r/axllent/mailpit 100M+ pulls and it offers multiarch images. Credentials can be configured via env var as well https://mailpit.axllent.org/docs/configuration/smtp/#passwords-via-environment. TLS support https://mailpit.axllent.org/docs/configuration/certificates/ and it offers a UI for local development

  13. linux-china commented on Sep 26, 2024

    @linux-china
    Author

    Using Mailpit with Spring Boot: https://dimitri.codes/spring-boot-mailpit/

    Mailpit is great with lots of feature, and it's light.

  14. psommersguter commented on Oct 6, 2025

    @psommersguter

    I would +1 this.

    For this example repository I also added Mailpit to showcase its usage.
    Although it is already very simple to add Mailpit through Testcontainers (as described in the previous mentioned blog post and as shown in the little configuration actually needed) there is an unexpected pitfall.

    The MailSenderAutoConfiguration requires the spring.mail.host as a condition to activate.

    Registering the spring.mail.host property through a DynamicPropertyRegistrar bean unexpectedly is not enough to trigger the auto-configuration although the "Precendence" section of the DynamicPropertyRegistrar JavaDoc would lead you to believe otherwise.
    You actually need to manually set this property (e.g. in a application.properties with a dummy value) to trigger the MailSenderAutoConfiguration.

    Therefore, having some sort of MailConnectionDetails and (in the best case) have all the infrastructure in place so we could annotate the Mailpit container bean with a @ServiceConnection would be great!

    Alternatively, as I think that the solution through a DynamicPropertyRegistrar is actually already quite elegant, adjusting the initialization to not need the dummy value for sring.mail.host would be fine as well in my option.

    I understand that my point of view is very Mailpit centric right now. Supporting all kinds of other mail-catching solutions through something like MailConnectionDetails might be more tricky.
    All other mentioned mail-catching projects don't seem to be in active development, though.

  15. modified the milestones: 3.x, 4.x on Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions