# Java

> Send your first email from Java using the built-in HttpClient against the SendPing API.

SendPing ships an official Java SDK (`co.sendping:sendping:5.1.1` on Maven Central, usable from Kotlin too) — see [Send emails with Java & Kotlin](https://www.sendping.co/docs/integrations/java) for the typed client — but you do not need it: this guide sends email by POSTing JSON to `https://www.sendping.co/api/emails`. The example below uses the `java.net.http.HttpClient` introduced in Java 11, so no dependencies are required.

## Prerequisites

- A SendPing [API key](https://www.sendping.co/docs/api-keys/overview).
- A [verified domain](https://www.sendping.co/docs/domains/managing) to send from.
- Java 11 or newer (for the built-in `HttpClient`).

## 1. Set your API key

**.env**

```sh
SENDPING_API_KEY=mb_xxxxxxxxx
```

## 2. Send emails using HTML

**Main.java**

```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("SENDPING_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@test.sendping.co\"],"
            + "\"subject\": \"it works!\","
            + "\"html\": \"<strong>hello world</strong>\""
            + "}";

        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("https://www.sendping.co/api/emails"))
            .header("Authorization", "Bearer " + apiKey)
            .header("Content-Type", "application/json")
            // Every SendPing 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());
    }
}
```

> **Note:** In Spring Boot, the simplest route is to add the `co.sendping:sendping` dependency and expose one `@Bean SendPing sendping()` 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](https://www.sendping.co/docs/api/emails-send) for all fields.
