Spring Boot¶
Guide for applications built with Spring Boot 3.x (Java, JVM). Mailexam connects via spring-boot-starter-mail and JavaMailSender auto-configuration.
What you need¶
- A Mailexam account and a project with SMTP credentials.
- JDK 17+ and a Spring Boot project (via start.spring.io or manually).
Copy from the welcome email (or dashboard) for your project:
YOUR_LOGIN— SMTP login (for example,xxxxx);YOUR_PASSWORD— SMTP password (a unique pair with the login);- host —
YOUR_LOGIN.mailexam.io(matches the login).
1. Dependencies¶
On start.spring.io select Spring Web and Java Mail Sender, or add to 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'
}
For Maven — the same artifacts spring-boot-starter-web and spring-boot-starter-mail.
2. Environment variables¶
.env file for local development (do not commit passwords to git) or variables in Run Configuration / CI:
MAILEXAM_LOGIN=YOUR_LOGIN
MAILEXAM_PASSWORD=YOUR_PASSWORD
MAILEXAM_PORT=587
MAIL_FROM=noreply@example.test
SMTP host in config: ${MAILEXAM_LOGIN}.mailexam.io.
Sender address
MAIL_FROM can be any test address — the message goes to Mailexam, not to a real recipient.
Alternative ports¶
3. Mail configuration¶
src/main/resources/application.yml:
spring:
mail:
host: ${MAILEXAM_LOGIN}.mailexam.io
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}
The same in application.properties:
spring.mail.host=${MAILEXAM_LOGIN}.mailexam.io
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}
For port 25 override mail.smtp.starttls.enable=false (see table above).
4. Sending service¶
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 endpoint¶
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 = "Mailexam test from Spring Boot";
}
}
}
@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");
}
}
Start and verify:
curl -X POST http://127.0.0.1:8080/mail/test \
-H 'Content-Type: application/json' \
-d '{"to":"user@example.test","subject":"Test","body":"Hello"}'
The message will appear in the Mailexam dashboard → your project → inbox.
6. Local development and CI¶
| Environment | Recommendation |
|---|---|
local |
variables in IDE or .env + envfile plugin in Gradle |
| CI | secrets MAILEXAM_LOGIN, MAILEXAM_PASSWORD in GitLab CI/CD Variables |
Example for .gitlab-ci.yml:
variables:
MAILEXAM_LOGIN: $MAILEXAM_LOGIN
MAILEXAM_PASSWORD: $MAILEXAM_PASSWORD
MAILEXAM_PORT: "587"
MAIL_FROM: "noreply@example.test"
After an integration test, verify delivery via the Mailexam API.
7. Common issues¶
TLS or connection error
spring.mail.hostmust be{login}.mailexam.io,username— the same login from the email.- Login and password are a pair from the email for one project.
Variables not substituted
- Run the application with
MAILEXAM_LOGINandMAILEXAM_PASSWORDset; in the IDE check Environment in the Run configuration.
Message not in the dashboard
- View the inbox of the same Mailexam project.
- Enable logging:
logging.level.org.springframework.mail=DEBUG.
See also¶
- Examples catalog
- Reference implementation (Spring Boot)
- Ktor — another JVM framework with Jakarta Mail
- Mailexam API documentation
- Spring Boot — Sending Email