Tutorial

Send Email with Spring Boot (JavaMailSender) via SMTP

spring-boot-starter-mail, five properties and a small service class — plain text and HTML.

By the Sendcorex team·Updated Nov 18, 2026·6 min read

1. Add the mail starter

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

2. Configure SMTP in application.properties

spring.mail.host=smtp.sendcorex.com
spring.mail.port=587
spring.mail.username=${SENDCOREX_API_KEY}
spring.mail.password=${SENDCOREX_API_KEY}
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Keep the key in an environment variable, never in the repository.

3. Send a plain-text email

@Service
public class MailService {
  private final JavaMailSender mailer;

  public MailService(JavaMailSender mailer) { this.mailer = mailer; }

  public void sendWelcome(String to) {
    SimpleMailMessage msg = new SimpleMailMessage();
    msg.setFrom("Acme <hello@yourdomain.com>");
    msg.setTo(to);
    msg.setSubject("Welcome to Acme");
    msg.setText("Thanks for signing up!");
    mailer.send(msg);
  }
}

4. Send HTML

MimeMessage mime = mailer.createMimeMessage();
MimeMessageHelper h = new MimeMessageHelper(mime, true, "UTF-8");
h.setFrom("hello@yourdomain.com", "Acme");
h.setTo(to);
h.setSubject("Your receipt");
h.setText("Your receipt is attached.", "<html><body><p>Your receipt is attached.</p></body></html>");
mailer.send(mime);

Pass both a plain-text and an HTML version, and send a full HTML document with <html> and <body> tags — fragment-only HTML is a common spam signal.

5. Authenticate your domain

Before going live, add your domain in Sendcorex and publish the SPF, DKIM and DMARC records it shows. Use the same sender name on every email, matching your brand. Our SPF generator and DMARC generator build the records for you.

Frequently asked questions

Which port should Spring Boot use for SMTP?

Port 587 with STARTTLS (mail.smtp.starttls.enable=true). Use 465 with mail.smtp.ssl.enable=true only if 587 is blocked on your network.

Why does JavaMailSender time out?

Usually the hosting provider blocks outbound SMTP ports. Try 2525 or 465, or call the REST Email API over HTTPS instead.

Send from Spring Boot in minutes

Create a free account, verify your domain and paste in the properties above.

No credit card required