Skip to content

Spring Cloud Alibaba

Guide for microservices built with Spring Cloud Alibaba on Spring Boot 3.5.x (Java, JVM). Mailexam connects via spring-boot-starter-mail and JavaMailSender — the same SMTP stack as in plain Spring Boot, inside a Spring Cloud Alibaba service.

What you need

  • A Mailexam account and a project with SMTP credentials.
  • JDK 17+ and a Spring Cloud Alibaba project (Gradle or Maven).

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

Add the Spring Cloud and Spring Cloud Alibaba BOMs, then mail and (optionally) Nacos discovery.

Gradle (build.gradle):

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

ext {
    set('springCloudVersion', '2025.0.0')
    set('springCloudAlibabaVersion', '2025.0.0.0')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
        mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaVersion}"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-mail'
    implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
}

For Maven — import the same BOMs and add the three starters.

Nacos optional for SMTP testing

Nacos discovery is included for a typical Spring Cloud Alibaba layout, but you can disable it locally (see section 3) and test mail without a Nacos server.

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
NACOS_DISCOVERY_ENABLED=false
NACOS_SERVER_ADDR=127.0.0.1:8848

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

MAILEXAM_PORT=587

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

MAILEXAM_PORT=2525

STARTTLS enabled, same as for 587.

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

3. Application configuration

Enable discovery on the main class:

@SpringBootApplication
@EnableDiscoveryClient
public class MailServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(MailServiceApplication.class, args);
    }
}

src/main/resources/application.yml:

spring:
  application:
    name: mail-service
  cloud:
    nacos:
      discovery:
        enabled: ${NACOS_DISCOVERY_ENABLED:false}
        server-addr: ${NACOS_SERVER_ADDR:127.0.0.1:8848}
  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}

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 Cloud Alibaba + Mailexam";
            }
            if (body == null || body.isBlank()) {
                body = "Mailexam test from Spring Cloud Alibaba";
            }
        }
    }

    @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:

./gradlew bootRun
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; keep NACOS_DISCOVERY_ENABLED=false without Nacos
CI secrets MAILEXAM_LOGIN, MAILEXAM_PASSWORD in pipeline variables

Example for GitLab CI:

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

After an integration test, verify delivery via the Mailexam API.

7. Common issues

TLS or connection error

  • spring.mail.host must be {login}.mailexam.io, username — the same login from the email.
  • Login and password are a pair from the email for one project.

Nacos connection errors on startup

  • Set NACOS_DISCOVERY_ENABLED=false for local SMTP testing.
  • When enabling discovery, ensure Nacos is reachable at NACOS_SERVER_ADDR.

Variables not substituted

  • Run the application with MAILEXAM_LOGIN and MAILEXAM_PASSWORD set; 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