Como enviar e-mails com Java / Spring
Envie e-mails do Spring com o `RestClient` (Spring 6+), chamando a API REST do Publiq.
Não há SDK oficial — chame a API REST do Publiq com o cliente HTTP da linguagem, a partir do servidor, com a API key no header Authorization.
import org.springframework.web.client.RestClient;
import java.util.Map;
public class PubliqService {
private final RestClient client = RestClient.create();
public void sendWelcome(String to, String name) {
client.post()
.uri("https://api.publiq.digital/v1/emails")
.header("Authorization", "Bearer " + System.getenv("PUBLIQ_API_KEY"))
.body(Map.of(
"from", "you@yourdomain.com",
"to", to,
"templateKey", "welcome-email",
"variables", Map.of("first_name", name)
))
.retrieve()
.toBodilessEntity();
}
}Boas práticas
- Chame o Publiq sempre do servidor — a API key vai no header
Authorization, nunca no cliente. - Em
429/5xx, retente com backoff exponencial (respeiteRetry-After). - Prefira
templateKeya HTML inline para manter o conteúdo versionado. - Trate os erros pelo campo
error.codeda resposta — veja Erros. - Envie de um domínio verificado — veja Domínios.