Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Environment:APIJSON APIJSON Demo SMTP

From Leeroopedia
Knowledge Sources
Domains Notification, Infrastructure, Testing
Last Updated 2026-10-14 00:00 GMT

Overview

SMTP mail server environment with Spring Boot Mail 3.2.5 (JavaMailSender), required for sending automated regression test report emails from the APIJSON Demo server.

Description

This environment provides SMTP email capability via Spring Boot's spring-boot-starter-mail module. The AutoTestRegressionScheduler uses JavaMailSender to send HTML-formatted test report emails after each scheduled regression test run. SMTP authentication and STARTTLS are enabled by default. Mail configuration is injected from application.yml using Spring's @Value and environment variable placeholders.

Usage

Use this environment for the AutoTestRegressionScheduler implementation, which sends test result notifications. If SMTP is not configured, the scheduler logs a warning and skips email sending, but test execution still proceeds.

System Requirements

Category Requirement Notes
OS Linux / macOS / Windows Any OS supporting JDK 17
Hardware Minimal Email sending is lightweight
Disk Minimal For email logging only

Dependencies

System Packages

  • Access to an SMTP server (e.g., Gmail, SendGrid, self-hosted Postfix)

Java Packages

  • `spring-boot-starter-mail` = 3.2.5

Credentials

The following environment variables must be set for email notifications:

  • `MAIL_HOST` — SMTP server hostname (e.g., `smtp.gmail.com`)
  • `MAIL_PORT` — SMTP server port (default: `587`)
  • `MAIL_USERNAME` — SMTP authentication username
  • `MAIL_PASSWORD` — SMTP authentication password
  • `MAIL_SMTP_AUTH` — Enable SMTP auth (default: `true`)
  • `MAIL_SMTP_STARTTLS_ENABLE` — Enable STARTTLS (default: `true`)
  • `AUTO_TEST_MAIL_FROM` — Sender email (defaults to `MAIL_USERNAME`)
  • `AUTO_TEST_MAIL_TO` — Recipient email(s), comma/semicolon separated

Quick Install

# Set environment variables (example with Gmail)
export MAIL_HOST=smtp.gmail.com
export MAIL_PORT=587
export MAIL_USERNAME=your-email@gmail.com
export MAIL_PASSWORD=your-app-password
export AUTO_TEST_MAIL_TO=team@example.com

# Start the APIJSON server — SMTP is auto-configured by Spring Boot

Code Evidence

SMTP configuration from `application.yml:98-108`:

spring:
  mail:
    host: ${MAIL_HOST:}
    port: ${MAIL_PORT:587}
    username: ${MAIL_USERNAME:}
    password: ${MAIL_PASSWORD:}
    properties:
      mail:
        smtp:
          auth: ${MAIL_SMTP_AUTH:true}
          starttls:
            enable: ${MAIL_SMTP_STARTTLS_ENABLE:true}

JavaMailSender usage in `AutoTestRegressionScheduler.java:195-221`:

private void sendReportEmail(TestExecutionResult result) {
    String[] recipients = parseRecipients(mailTo);
    if (recipients.length <= 0) {
        Log.w(TAG, "未配置 apijson.auto-test.mail.to,跳过发送测试报告邮件");
        return;
    }
    JavaMailSender mailSender = mailSenderProvider.getIfAvailable();
    if (mailSender == null) {
        Log.e(TAG, "未找到 JavaMailSender,无法发送测试报告邮件");
        return;
    }
    var message = mailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message, false, StandardCharsets.UTF_8.name());
    helper.setFrom(mailFrom.trim());
    helper.setTo(recipients);
    helper.setSubject(buildMailSubject(result));
    helper.setText(buildMailHtml(result), true);
    mailSender.send(message);
}

Mail dependency from `pom.xml:343-347`:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>3.2.5</version>
</dependency>

Common Errors

Error Message Cause Solution
`未配置 apijson.auto-test.mail.to,跳过发送` `AUTO_TEST_MAIL_TO` not set Set `AUTO_TEST_MAIL_TO` environment variable
`Authentication failed exception` Wrong SMTP credentials Verify `MAIL_USERNAME` and `MAIL_PASSWORD`
`Could not convert socket to TLS` STARTTLS not supported by server Set `MAIL_SMTP_STARTTLS_ENABLE=false` or use port 465
`未找到 JavaMailSender` spring-boot-starter-mail not on classpath Ensure `spring-boot-starter-mail` dependency is present

Compatibility Notes

  • Gmail App Passwords: Gmail requires app-specific passwords, not account passwords. Enable 2FA and generate an app password.
  • Graceful degradation: If `MAIL_HOST` is empty or `JavaMailSender` is unavailable, the scheduler skips email and logs a warning — test execution is not affected.
  • HTML email: Reports are sent as HTML (helper.setText(html, true)) with escaped content to prevent XSS in email clients.
  • ObjectProvider injection: JavaMailSender is injected via ObjectProvider, making it optional at runtime.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment