Send Emails with Java
Learn how to send transactional emails using PostStack and Java.
1. Install the SDK
bash
// No external dependencies — uses Java 11+ HttpClient2. Initialize the client
java
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;
HttpClient client = HttpClient.newHttpClient();
String apiKey = "sk_live_...";3. Send an email
java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class SendEmail {
public static void main(String[] args) throws Exception {
String apiKey = System.getenv("POSTSTACK_API_KEY");
String json = """
{
"from": "hello@yourdomain.com",
"to": "user@example.com",
"subject": "Hello from Java!",
"html": "<h1>Welcome!</h1>"
}
""";
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.poststack.dev/v1/emails"))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}Notes
- Uses the Java 11+ built-in HttpClient — no Maven/Gradle dependencies needed
Ready to send emails with Java?
Create a free account and get your API key in under a minute.