# Send email from Spring Boot

Add the Java SDK, make one `Mailset` bean, and take delivery events in a `@PostMapping` that reads the body as `byte[]`. There are no `spring.mail` properties to set: it calls Mailset over HTTPS.

## Install the Java SDK

Maven or Gradle. It needs Java 17 or later, the same floor as Spring Boot 3.

Maven:

```xml
<dependency>
    <groupId>app.mailset</groupId>
    <artifactId>java</artifactId>
    <version>0.7.0</version>
</dependency>
```

Gradle:

```kotlin
implementation("app.mailset:java:0.7.0")
```

## Send your first email from Spring Boot

`Mailset.fromEnv()` reads `MAILSET_KEY`; return it from a `@Bean` method and inject it where you send. `Message.builder()` sets each field, `.deliverBy("10m")` included, and `send` answers a `Sent` whose `id()` you can store.

```java
import app.mailset.Mailset;
import app.mailset.Message;
import app.mailset.Sent;

// Reads MAILSET_KEY from the environment.
Mailset mailset = Mailset.fromEnv();

Sent sent = mailset.send(Message.builder()
        .from("Acme <hello@example.com>")
        .to("ana@example.org")
        .subject("Your Acme code")
        .text("Your code is 123456.")
        .html("<p>Your code is <b>123456</b>.</p>")
        .deliverBy("10m")
        .build());

System.out.println(sent.id() + " " + sent.status());
```

## Verify webhooks in a controller

The method takes `@RequestBody byte[] body` and the `Mailset-Signature` header. A mapped object would be JSON Spring had already parsed, not the bytes the signature covers. `Webhooks.verify(body, signature, secret)` answers the events, a hard bounce matches `MailEvent.Bounced`, and `ResponseEntity.noContent()` marks the batch kept. A `MailsetException` answers 401 with its code.

```java
import app.mailset.MailEvent;
import app.mailset.MailsetException;
import app.mailset.Webhooks;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

// The signature covers the body exactly as it came:
// take it as bytes. A mapped object would lose the
// bytes it was made over.
@PostMapping("/mailset/events")
public ResponseEntity<String> events(
        @RequestBody byte[] body,
        @RequestHeader(value = "Mailset-Signature",
                required = false) String signature) {
    try {
        List<MailEvent> events =
                Webhooks.verify(body, signature, secret);
        for (MailEvent event : events) {
            if (event instanceof MailEvent.Bounced b
                    && b.bounce().kind().equals("hard"))
                stopWritingTo(b.recipient());
        }
        // 2xx once the events are kept. Until then
        // Mailset posts the batch again.
        return ResponseEntity.noContent().build();
    } catch (MailsetException error) {
        return ResponseEntity.status(401)
                .body(error.getCode());
    }
}
```

In the SDK’s tests: compiled against Spring stand-ins, and run.

## Send from your own domain

Your product sends as your own domain either way. What changes is where your team’s mail lives.

- **Sending only.** Your team’s mail stays at Google, Microsoft or wherever it is now. Mailset asks for DKIM, a bounce host and DMARC, and leaves your MX alone. [Send from a domain whose mail stays elsewhere](https://mailset.app/docs/recipes/sending-only)
- **Mailboxes too.** Your team’s mail moves to Mailset, with a real mailbox for every address on the domain and your product’s email on the same bill. [Set up a domain](https://mailset.app/guide/onboarding)

```bash
mailset domains add example.com --sending
mailset domains apply example.com --sending
mailset domains check example.com
mailset keys create --send --domain example.com --from hello@example.com --name "Acme API"
```

## What the Java SDK’s build checks

Each line comes from the SDK’s own build, which fails when one stops being true.

- **Java 17+**: The oldest its manifest allows (its manifest)
- **No dependencies**: Nothing else to install beside it (its manifest)
- **Typed throughout**: Immutable classes, each with a builder (a test)
- **Sync and async**: Mailset, and async() for futures (a test)

## Where to go from here

- [The Java SDK](https://mailset.app/docs/sdk/java): Every task, from the client’s options to errors and retries.
- [Send email from Java](https://mailset.app/send-email/java): The Java guide, with no framework around it.
- [Webhooks](https://mailset.app/docs/webhooks): The events Mailset posts about your product’s email, and how to check a request’s signature.
- [Give sign-in codes a deadline](https://mailset.app/docs/recipes/deadlines): Use deliverBy so a code that would arrive late is never sent.
- [Retry without sending twice](https://mailset.app/docs/recipes/retries): Make a send safe to repeat with an idempotency key or a Message-ID.
- [Handle bounces](https://mailset.app/docs/recipes/bounces): Tell a dead address from a full inbox, and keep your list clean.
- [Send many messages at once](https://mailset.app/docs/recipes/batches): Up to 100 messages a request, each with its own answer.

## Product email on every plan

Both include 10,000 emails a month, your product’s and your team’s together. Past that it’s $0.40 per 1,000 with pay as you go. Every plan and price: https://mailset.app/pricing

## Sending from Spring Boot, asked and answered

### Do I need spring-boot-starter-mail?

No. That starter drives `JavaMailSender` over SMTP. The Mailset client needs only the JDK and sends over HTTPS.

### Can it be a singleton bean?

Yes. It holds one credential and is safe to share between threads, which is all a singleton bean asks of it.

### Does Spring Security block the hook?

With CSRF protection on, it refuses the POST. Exempt the hook’s path from CSRF; the signature check guards it instead.

### What about WebFlux?

`mailset.async()` answers a `CompletableFuture` for each call, which `Mono.fromFuture` wraps without blocking a thread.

## Try it on your own domain

7 days free, with everything on. Make a key, run the example above and watch the receipt come back. Start at https://mailset.app/start

---

© 2026 Intenseloop, Inc. Mailset’s site, docs and guides are copyrighted. You may read them and use their code to build with Mailset. Don’t republish them or use them to build a copy of Mailset. Terms: https://mailset.app/terms#content