Guides / Spring Boot

Test Spring Boot emails locally

RustMail is a local SMTP mail catcher. Your Java app keeps sending mail the way it does in production, and every message lands in a web UI instead of a real inbox. Four steps, about two minutes.

  1. Start RustMail

    One container, SMTP on port 1025 and the web UI on 8025.

    docker run -p 1025:1025 -p 8025:8025 smyile/rustmail:latest
  2. Point Spring Boot at it

    In application.properties, send mail to localhost:1025 with no TLS and no credentials.

    spring.mail.host=localhost
    spring.mail.port=1025
    spring.mail.properties.mail.smtp.auth=false
    spring.mail.properties.mail.smtp.starttls.enable=false
  3. Send a test email

    Then open localhost:8025: the message is already in the inbox, with its HTML, headers and links.

    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("[email protected]");
    message.setTo("[email protected]");
    message.setSubject("Welcome");
    message.setText("Hello from Spring Boot");
    mailSender.send(message);
  4. Assert on it in CI

    In GitHub Actions, start RustMail, run tests that send mail with the application.properties settings from step 2, then fail the job when the email never arrives.

    - name: Start RustMail on localhost:1025
      uses: rustmailapp/rustmail-action@v1
      with:
        mode: start
    
    - name: Run the tests that send mail over SMTP
      run: ./mvnw verify
      env:
        SPRING_MAIL_HOST: "localhost"
        SPRING_MAIL_PORT: "1025"
    
    - name: Fail if the welcome email never arrived
      uses: rustmailapp/rustmail-action@v1
      with:
        mode: assert
        assert-count: 1
        assert-subject: "Welcome"

Good to know

  • Put these properties in a dev or test profile so production keeps its real SMTP settings.

Other frameworks

LaravelDjangoRuby on RailsNode.js