Quick setup examples
Java
Send your first email from Java using the built-in HttpClient against the MailBlastr API.
MailBlastr ships an official Java SDK (com.mailblastr:mailblastr:5.0.0 on Maven Central, usable from Kotlin too) — see Send emails with Java & Kotlin for the typed client — but you do not need it: this guide sends email by POSTing JSON to https://www.mailblastr.com/api/emails. The example below uses the java.net.http.HttpClient introduced in Java 11, so no dependencies are required.
Prerequisites
- A MailBlastr API key.
- A verified domain to send from.
- Java 11 or newer (for the built-in
HttpClient).
1. Set your API key
.env
MAILBLASTR_API_KEY=mb_xxxxxxxxx2. Send emails using HTML
Main.java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main {
public static void main(String[] args) throws Exception {
String apiKey = System.getenv("MAILBLASTR_API_KEY");
// Written as concatenated literals, not a text block: text blocks are a
// standard language feature only from Java 15, and this page targets 11.
String body = "{"
+ "\"from\": \"Acme <onboarding@yourdomain.com>\","
+ "\"to\": [\"delivered@mailblastr.dev\"],"
+ "\"subject\": \"it works!\","
+ "\"html\": \"<strong>hello world</strong>\""
+ "}";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://www.mailblastr.com/api/emails"))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
// Every MailBlastr API request must carry a User-Agent; HttpClient
// sends a default one, but set your own so it identifies your app.
.header("User-Agent", "my-app/1.0")
.POST(HttpRequest.BodyPublishers.ofString(body))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
}
}In Spring Boot, the simplest route is to add the
com.mailblastr:mailblastr dependency and expose one @Bean Mailblastr mailblastr() built from your API key — the artifact has zero transitive dependencies, so it will not clash with your HTTP stack. Prefer no dependency? RestClient or WebClient work against the same URL with the same Bearer header. See Send an email for all fields.