跳转至

Spring Boot

适用于 Spring Boot 3.x(Java,JVM)应用的集成指南。Mailexam 通过 spring-boot-starter-mailJavaMailSender 自动配置接入。

前置条件

  • Mailexam 账户,以及已创建并具备 SMTP 凭据的项目
  • JDK 17+ 及 Spring Boot 项目(通过 start.spring.io 或手动创建)。

从项目的欢迎邮件(或控制台)中复制:

  • YOUR_LOGIN — SMTP 登录名(例如 xxxxx);
  • YOUR_PASSWORD — SMTP 密码(与登录名成对的唯一凭据);
  • 主机 — YOUR_LOGIN.mailexam.cn登录名一致)。

1. 依赖

start.spring.io 选择 Spring WebJava Mail Sender,或在 build.gradle 中添加:

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.4.0'
    id 'io.spring.dependency-management' version '1.1.6'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-mail'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Maven 使用相同构件 spring-boot-starter-webspring-boot-starter-mail

2. 环境变量

本地开发的 .env 文件(勿将密码提交到 git),或运行配置 / CI 中的变量:

MAILEXAM_LOGIN=YOUR_LOGIN
MAILEXAM_PASSWORD=YOUR_PASSWORD
MAILEXAM_PORT=587
MAIL_FROM=noreply@example.test

配置中的 SMTP 主机:${MAILEXAM_LOGIN}.mailexam.cn

发件人地址

MAIL_FROM 可以是任意测试地址——邮件进入 Mailexam,而非真实收件人。

备选端口

MAILEXAM_PORT=587

spring.mail.properties.mail.smtp.starttls.enable=true

MAILEXAM_PORT=2525

启用 STARTTLS,与 587 相同。

MAILEXAM_PORT=25
spring.mail.properties.mail.smtp.starttls.enable=false

3. 邮件配置

src/main/resources/application.yml

spring:
  mail:
    host: ${MAILEXAM_LOGIN}.mailexam.cn
    port: ${MAILEXAM_PORT:587}
    username: ${MAILEXAM_LOGIN}
    password: ${MAILEXAM_PASSWORD}
    properties:
      mail.smtp.auth: true
      mail.smtp.starttls.enable: true

mail:
  from: ${MAIL_FROM:noreply@example.test}

application.properties 中相同配置:

spring.mail.host=${MAILEXAM_LOGIN}.mailexam.cn
spring.mail.port=${MAILEXAM_PORT:587}
spring.mail.username=${MAILEXAM_LOGIN}
spring.mail.password=${MAILEXAM_PASSWORD}
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
mail.from=${MAIL_FROM:noreply@example.test}

25 端口须覆盖 mail.smtp.starttls.enable=false(见上表)。

4. 发送服务

package com.example.demo.mail;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@Service
public class MailService {

    private final JavaMailSender mailSender;
    private final String from;

    public MailService(JavaMailSender mailSender, @Value("${mail.from}") String from) {
        this.mailSender = mailSender;
        this.from = from;
    }

    public void sendTest(String to, String subject, String body) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, "UTF-8");
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(body, false);
        mailSender.send(message);
    }
}

5. REST 端点

package com.example.demo.web;

import com.example.demo.mail.MailService;
import jakarta.mail.MessagingException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class MailController {

    private final MailService mailService;

    public MailController(MailService mailService) {
        this.mailService = mailService;
    }

    public record SendRequest(
            String to,
            String subject,
            String body
    ) {
        public SendRequest {
            if (to == null || to.isBlank()) {
                to = "user@example.test";
            }
            if (subject == null || subject.isBlank()) {
                subject = "Spring + Mailexam";
            }
            if (body == null || body.isBlank()) {
                body = "来自 Spring Boot 的 Mailexam 测试";
            }
        }
    }

    @PostMapping("/mail/test")
    public Map<String, String> sendTest(@RequestBody SendRequest request) throws MessagingException {
        mailService.sendTest(request.to(), request.subject(), request.body());
        return Map.of("status", "ok");
    }
}

启动并验证:

./gradlew bootRun
curl -X POST http://127.0.0.1:8080/mail/test \
  -H 'Content-Type: application/json' \
  -d '{"to":"user@example.test","subject":"测试","body":"你好"}'

邮件将出现在 Mailexam 控制台 → 您的项目 → 收件箱。

6. 本地开发与 CI

环境 建议
local IDE 变量或 .env + Gradle envfile 插件
CI GitLab CI/CD Variables 中的密钥 MAILEXAM_LOGINMAILEXAM_PASSWORD

.gitlab-ci.yml 示例:

variables:
  MAILEXAM_LOGIN: $MAILEXAM_LOGIN
  MAILEXAM_PASSWORD: $MAILEXAM_PASSWORD
  MAILEXAM_PORT: "587"
  MAIL_FROM: "noreply@example.test"

集成测试后,通过 Mailexam API 验证投递。

7. 常见问题

TLS 或连接错误

  • spring.mail.host 须为 {login}.mailexam.cnusername — 邮件中的同一登录名。
  • 登录名和密码为同一项目的邮件成对凭据

变量未替换

  • 在已设置 MAILEXAM_LOGINMAILEXAM_PASSWORD 的情况下运行应用;在 IDE 中检查运行配置的 Environment。

控制台中看不到邮件

  • 查看同一 Mailexam 项目的收件箱。
  • 启用日志:logging.level.org.springframework.mail=DEBUG

参见