Email Testing
Email Testing
Section titled âEmail TestingâOverview
Section titled âOverviewâHatchgrid supports multiple email testing solutions for development and testing environments:
- GreenMail - A lightweight, in-memory email server that supports all major email protocols
- MailDev - A simple SMTP server with a web interface for easy email testing
You can choose the email testing solution that best fits your needs and switch between them using the provided script.
Both the Spring Boot application and Keycloak are configured to use GreenMail for sending emails during development and testing.
Configuration
Section titled âConfigurationâDocker Compose Setup
Section titled âDocker Compose SetupâGreenMail is configured as a Docker service in the projectâs infrastructure:
services: greenmail: image: greenmail/standalone:${GREENMAIL_VERSION} container_name: greenmail environment: - GREENMAIL_OPTS=-Dgreenmail.setup.test.all -Dgreenmail.hostname=0.0.0.0 -Dgreenmail.auth.disabled -Dgreenmail.verbose ports: - "3025:3025" # SMTP - "3110:3110" # POP3 - "3143:3143" # IMAP - "3465:3465" # SMTPS - "3993:3993" # IMAPS - "3995:3995" # POP3S - "8080:8080" # Web interface
Environment Variables
Section titled âEnvironment VariablesâThe GreenMail version is configured in .env
:
GREENMAIL_VERSION=2.0.0
Available Protocols and Ports
Section titled âAvailable Protocols and PortsâGreenMail supports multiple email protocols for comprehensive testing:
Protocol | Port | Description |
---|---|---|
SMTP | 3025 | Simple Mail Transfer Protocol (sending emails) |
POP3 | 3110 | Post Office Protocol v3 (retrieving emails) |
IMAP | 3143 | Internet Message Access Protocol (managing emails) |
SMTPS | 3465 | SMTP over SSL/TLS |
IMAPS | 3993 | IMAP over SSL/TLS |
POP3S | 3995 | POP3 over SSL/TLS |
Web UI | 8080 | Web interface for email management |
Starting GreenMail
Section titled âStarting GreenMailâGreenMail starts automatically when you run the main Docker Compose setup:
docker compose up -d
Or start only GreenMail:
docker compose up -d greenmail
Web Interface
Section titled âWeb InterfaceâAccess the GreenMail web interface at: http://localhost:8080
The web interface allows you to:
- View all received emails
- Inspect email headers and content
- Manage mailboxes and users
- Monitor email traffic
Spring Boot Configuration
Section titled âSpring Boot ConfigurationâConfigure your Spring Boot application to use GreenMail for email testing:
spring: mail: host: localhost port: 3025 protocol: smtp properties: mail: smtp: auth: false starttls: enable: false
Testing Email Functionality
Section titled âTesting Email FunctionalityâIntegration Tests
Section titled âIntegration TestsâUse GreenMail in your integration tests to verify email sending:
@SpringBootTest@Testcontainersclass EmailServiceIntegrationTest {
@Container companion object { val greenMail = GenericContainer("greenmail/standalone:2.0.0") .withExposedPorts(3025, 8080) .withEnv("GREENMAIL_OPTS", "-Dgreenmail.setup.test.all -Dgreenmail.hostname=0.0.0.0 -Dgreenmail.auth.disabled") }
@Test fun `should send email successfully`() { // Test email sending functionality // Verify emails are received in GreenMail }}
Manual Testing
Section titled âManual Testingâ- Start the application with GreenMail running
- Trigger email functionality in your application
- Check the GreenMail web interface at http://localhost:8080
- Verify emails are received and formatted correctly
Configuration Options
Section titled âConfiguration OptionsâGreenMail Options
Section titled âGreenMail OptionsâThe GREENMAIL_OPTS
environment variable configures GreenMail behavior:
-Dgreenmail.setup.test.all
: Sets up all email protocols-Dgreenmail.hostname=0.0.0.0
: Binds to all network interfaces-Dgreenmail.auth.disabled
: Disables authentication for testing-Dgreenmail.verbose
: Enables verbose logging
Health Checks
Section titled âHealth ChecksâGreenMail includes a health check that verifies the SMTP port is accessible:
healthcheck: test: ["CMD", "echo", "quit", "|", "telnet", "localhost", "3025"] interval: 10s timeout: 5s retries: 10 start_period: 30s
This health check uses telnet
to connect to the SMTP port and sends the SMTP âquitâ command, providing a more reliable check than simply testing if the port is open.
Best Practices
Section titled âBest PracticesâDevelopment Environment
Section titled âDevelopment Environmentâ- Use GreenMail for all email testing - Never use real email services in development
- Check the web interface regularly - Monitor emails during development
- Clear emails between tests - Reset GreenMail state for clean test runs
Testing Strategy
Section titled âTesting Strategyâ- Integration tests should use GreenMail - Verify complete email workflows
- Unit tests should mock email services - Focus on business logic
- Test email content and formatting - Verify HTML and text versions
- Test email delivery failures - Ensure proper error handling
Security Considerations
Section titled âSecurity Considerationsâ- Never use GreenMail in production - Itâs designed for testing only
- Disable authentication in test environments - Simplifies testing setup
- Use network isolation - Keep GreenMail in backend network only
Troubleshooting
Section titled âTroubleshootingâCommon Issues
Section titled âCommon IssuesâPort Conflicts
Section titled âPort ConflictsâIf port 8080 is already in use:
# Check what's using port 8080lsof -i :8080
# Stop conflicting services or change GreenMail port mapping
Connection Refused
Section titled âConnection RefusedâEnsure GreenMail is running and healthy:
# Check container statusdocker compose ps greenmail
# Check container logsdocker compose logs greenmail
# Test SMTP connectionecho quit | telnet localhost 3025
Emails Not Appearing
Section titled âEmails Not Appearingâ- Check application email configuration points to localhost:3025
- Verify GreenMail logs for incoming connections
- Ensure authentication is disabled in test configuration
Debugging
Section titled âDebuggingâEnable verbose logging in GreenMail:
environment: - GREENMAIL_OPTS=-Dgreenmail.setup.test.all -Dgreenmail.hostname=0.0.0.0 -Dgreenmail.auth.disabled -Dgreenmail.verbose -Dgreenmail.debug
Keycloak Email Configuration
Section titled âKeycloak Email ConfigurationâKeycloak is configured to use GreenMail for sending emails such as account verification, password reset, and other notifications. The configuration is defined in the Keycloak realm configuration file:
"smtpServer": { "replyToDisplayName": "Hatchgrid", "starttls": "false", "auth": "false", "envelopeFrom": "", "ssl": "false", "password": "secret", "port": "3025", "replyTo": "noreply@hatchgrid.local", "host": "greenmail", "from": "noreply@hatchgrid.local", "fromDisplayName": "Hatchgrid Development", "user": "developer"}
Key Settings
Section titled âKey Settingsâ- Host:
greenmail
(Docker service name) - Port:
3025
(SMTP port) - Authentication: Disabled for development (
"auth": "false"
) - TLS/SSL: Disabled for development (
"starttls": "false"
,"ssl": "false"
) - From Address:
noreply@hatchgrid.local
- From Display Name:
Hatchgrid Development
Testing Keycloak Emails
Section titled âTesting Keycloak EmailsâTo test Keycloak email functionality:
-
Ensure both GreenMail and Keycloak are running:
Terminal window docker compose up -d greenmail keycloak -
Trigger an email-sending action in Keycloak:
- Password reset
- New user registration
- Email verification
-
Check the GreenMail web interface at http://localhost:8080 to view the sent emails
Switching Between Email Servers
Section titled âSwitching Between Email ServersâHatchgrid provides a script to easily switch between GreenMail and MailDev:
./scripts/switch-mail-server.sh [greenmail|maildev]
This script will:
- Stop any running email servers
- Start the requested email server
- Display configuration information