Viesti Sign in

Send email from Spring Boot with JavaMailSender

Spring Boot auto-configures a JavaMailSender from spring.mail.* properties when spring-boot-starter-mail is on the classpath. This guide sets it up for STARTTLS on port 587. Tested with Spring Boot 4.1 on Java 21.

Before you start

  1. Sign in and verify the domain you send from. Viesti accepts a message only when its From address is on a domain verified in your workspace and the envelope sender (MAIL FROM), which most libraries take from the From address, is on the same domain. DNS setup lists the records.
  2. Create an API key on the Credentials page of the dashboard. It is shown once. The key is the SMTP password; the username is apikey.
Setting Value
Host smtp.viesti.email
Port 587 with STARTTLS, or 465 with TLS from the first byte
Username apikey
Password Your API key: mr_ followed by 64 hex characters
Authentication PLAIN, offered only once the connection is encrypted

Add the starter

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

With Gradle: implementation 'org.springframework.boot:spring-boot-starter-mail'.

Configure

In src/main/resources/application.properties:

spring.mail.host=smtp.viesti.email
spring.mail.port=587
spring.mail.username=apikey
spring.mail.password=${VIESTI_API_KEY}
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000
app.mail.from=${MAIL_FROM}

Spring resolves ${VIESTI_API_KEY} and ${MAIL_FROM} from environment variables. starttls.enable upgrades the connection; starttls.required makes Jakarta Mail fail instead of continuing without TLS. The timeouts are in milliseconds; Spring Boot's documentation notes that some defaults are infinite.

export VIESTI_API_KEY="mr_your_api_key"
export MAIL_FROM="Acme <[email protected]>"

Send an email

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class ReceiptMailer {

    private final JavaMailSender mailSender;
    private final String from;

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

    public void sendReceipt(String to) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject("Your receipt");
        message.setText("Thanks for your order.");
        mailSender.send(message);
    }
}

Test it

Add a runner to your application class that sends one message at startup, run the application, and remove the runner once the message shows up on the Activity page:

package com.example.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    CommandLineRunner sendTestEmail(ReceiptMailer mailer) {
        return args -> mailer.sendReceipt("[email protected]");
    }
}

A refusal fails startup with a MailException carrying the server's reply.

Port 465

spring.mail.port=465
spring.mail.ssl.enabled=true

spring.mail.ssl.enabled sets mail.smtp.ssl.enable, so TLS starts before the SMTP greeting. The STARTTLS properties are ignored on a connection that is already encrypted.

Common errors

Error Cause and fix
MailSendException: Failed messages: org.eclipse.angus.mail.smtp.SMTPSendFailedException: 502 5.7.0 Please authenticate first starttls.enable is missing. Viesti offers AUTH only after TLS, so Jakarta Mail skipped authentication. Add both STARTTLS properties.
Mail server connection failed … javax.net.ssl.SSLException: Unsupported or unrecognized SSL message spring.mail.ssl.enabled=true on port 587. Use it only with 465.
Mail server connection failed … Exception reading response; java.net.SocketTimeoutException: Read timed out Port 465 without spring.mail.ssl.enabled=true: Jakarta Mail waits for a greeting that only comes after TLS.

Replies from Viesti

Whatever the library, its error includes the server's reply. These are the ones caused by your setup or account:

Reply Meaning
535 5.7.8 Authentication failed The API key is wrong, revoked or empty. After ten failed attempts from one client address within ten minutes, that address is refused for ten minutes with 454 Too many failed authentications; retry later.
525 5.7.13 Workspace suspended; contact support The key is valid, but the workspace is suspended or under review. Write to [email protected]; a new key does not help.
550 5.0.0 sending not permitted The From domain is not verified in this workspace, the envelope sender is on a different domain than the From header, or the workspace is not active.
550 5.0.0 recipient suppressed The recipient is on your suppression list after a bounce or complaint; see Bounces.
451 4.0.0 submission quota exceeded The free plan's 75 emails per UTC day or 2,000 per UTC month are used up; see Limits. It is temporary: retry after the reset or upgrade.
452 4.5.3 Maximum limit of 50 recipients reached One message can have at most 50 recipients. Split the send.
552 5.0.0 message too large The message, attachments included, is over 10 MB.
554 5.0.0 rejected by content audit Content screening found the message in violation of the Terms of Service. It is not relayed.

A 250 reply means Viesti accepted the message. Content screening runs before relay; mail it flags is held for review and shows as quarantined on the Activity page.

Next steps

  • DNS setup: DKIM, SPF, DMARC and return-path records for your domain.
  • SMTP reference: ports, TLS and authentication in detail.
  • HTTP API: send the same message as JSON over HTTPS.
  • Limits and Bounces: quotas, message size and suppressions.

Properties are from the Spring Boot Sending Email reference and application properties appendix; tested with Spring Boot 4.1.1 and Angus Mail 2.0.5. Checked 26 September 2026.