Guide
How to send email with Java
Send email from Java 17+ with the built-in HttpClient and the REST API - no JavaMail, no SMTP.
In short
Create a Sendpository account, verify your domain, put your API key inSENDPOSITORY_API_KEY, and send with the code below - one HTTPS request per email, from server-side code only.1. Verify your sending domain
Add your domain in the dashboard and publish the three DNS records it shows - SPF and a bounce record on an sp. subdomain, and a DKIM key on your apex. Your existing records are never edited. Verification usually lands in a few minutes, and you can set up the code below while you wait.
2. Create a key
Create an API key with the sending permission. It is shown once, so put it straight into your secrets. Read the key with System.getenv("SENDPOSITORY_API_KEY"); in Spring, bind it from application properties backed by an environment variable.
3. Send an email
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
public class SendEmail {
private static final HttpClient HTTP = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
public static void main(String[] args) throws Exception {
// Use Jackson or Gson in real code; a text block keeps this example dependency-free.
String json = """
{
"from": "Your App <hello@mail.yourdomain.com>",
"to": ["customer@example.com"],
"subject": "Your receipt",
"html": "<p>Thanks for your order.</p>"
}
""";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.sendpository.com/v1/emails"))
.timeout(Duration.ofSeconds(10))
.header("Authorization", "Bearer " + System.getenv("SENDPOSITORY_API_KEY"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = HTTP.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() >= 400) {
throw new IllegalStateException("Send failed: " + response.body());
}
System.out.println(response.body()); // {"id":"..."}
}
}A success returns { "id": "..." } - the message’s id for looking it up later. A failure returns { "error": { "type", "message" } } with a 4xx or 5xx status.
Where the send belongs
Off the request thread for anything user-facing. `HttpClient.send` is blocking; `sendAsync` returns a `CompletableFuture` you can hand to your own executor, which keeps the servlet thread free.
Build one `HttpClient` and share it. It is thread-safe and holds the connection pool, so a new instance per call is a new pool per call.
Testing it
- Always set a timeout on the request builder - the default is no timeout at all, and a stalled socket holds the thread indefinitely.
- Read the key from the environment rather than a properties file that ships inside the jar.
Things to know
- Reuse one HttpClient for the whole application - it pools connections.
- Build the body with Jackson or Gson so user input is escaped; never concatenate strings into JSON.
- In Spring Boot, call it from an @Async method or a queue consumer so a slow send never holds up a request.
- Add an Idempotency-Key header when the call can be retried, so a retry never sends twice.
Questions
send or sendAsync?
`sendAsync` for anything user-facing - `send` blocks the calling thread for the whole round trip. Share one `HttpClient` across the application: it is thread-safe and owns the connection pool, so a new instance per call is a new pool per call.
How do I send email with Java?
Verify your sending domain, create an API key, put it in an environment variable, and POST the message as JSON to https://api.sendpository.com/v1/emails from server-side Java code. The example on this page runs as written once the domain is verified.
Can I send email with Java without SMTP?
Yes. This is plain HTTPS, so Java needs no mail library and no SMTP port - which matters on hosts that block outbound 25 and 587. Failures come back as a status code on the call instead of as a bounce message hours later.
More guides
Send email with Go
Send email from Go with net/http and the REST API.
Send email with Python
Send email from Python with requests and the REST API.
Send your first email in minutes
6,000 emails a month (up to 200 a day), free forever. No card needed.