Email API Integration Guide with Java
Minimum code that you need to integrate Mailazy with your Java application
Prerequisites
You need to complete these given prerequisites, you can skip the step if you have already completed.
- Sign up for a Mailazy account.
- Complete Domain Authentication.
- Generate the Mailazy Access Key
Integrate Mailazy with Java
Start Sending mail using the code provided below:
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Main {
public static void main(String[] args) throws JSONException {
JSONObject json = new JSONObject();
json.put("to", new JSONArray().put("recipient@example.com"));
json.put("from", "example@sender.com");
json.put("subject", "<SUBJECT>");
JSONArray content = new JSONArray();
content.put(new JSONObject().put("type", "text/plain").put("value", "hello world!"));
content.put(new JSONObject().put("type", "text/html").put("value", "<b>hello world!</b>"));
json.put("content", content);
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.mailazy.com/v1/mail/send"))
.header("Content-Type", "application/json")
.header("X-Api-Key", "<API-KEY>")
.header("X-Api-Secret", "<API-SECRET>")
.POST(HttpRequest.BodyPublishers.ofString(json.toString()))
.build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
}