言語別 SDK
Java
Java から Sendery の公開済みテンプレートを送信します。
インストール
<dependency>
<groupId>co.sendery</groupId>
<artifactId>sendery-java</artifactId>
<version>0.1.4</version>
</dependency>動作環境
Java 17 以上が必要です。
設定
公開済みテンプレートを選び、プロジェクトの API キーを作成します。キーはサーバーの環境変数 SENDERY_API_KEY に保存してください。
Shell
export SENDERY_API_KEY="your_project_api_key"メールを送信
your-template を公開済みテンプレートのキーに、data をそのテンプレートの変数に置き換えてください。
受付済みメールの id と status が返ります。
Java
import co.sendery.Sendery;
import java.util.Map;
public class SendEmail {
public static void main(String[] args) {
var sendery = new Sendery(System.getenv("SENDERY_API_KEY"));
var receipt = sendery.send("[email protected]", "your-template", Map.of(
"name", "Alex",
"action_url", "https://example.com/start"
));
System.out.println(receipt.id());
}
}メールの状態を取得
返された ID で配信状態を確認します。 SendReceipt には errorCode()、createdAt()、submittedAt() もあります。呼び出しはリクエスト完了までブロックします。
Java
var message = sendery.get(receipt.id());
System.out.println(message.status());送信を再試行
一時的なエラーには retry(3) で最大 3 回追加で試行できます。毎回同じ冪等性キーとメールのデータを使ってください。
Java
var email = sendery.prepare("[email protected]", "your-template", Map.of(
"name", "Alex", "action_url", "https://example.com/start"
), null, "your-idempotency-key");
var receipt = email.retry(3).send();エラー処理
Java
try {
var receipt = email.retry(3).send();
System.out.println(receipt.id());
} catch (co.sendery.SenderyException error) {
System.err.println(error.status() + ": " + error.code());
// error.errors() contains field-level validation errors.
// error.retryAfter() is a delay in seconds, when provided.
throw error;
}特定のバージョンを送信
公開履歴のバージョンを指定すると、テンプレートの更新後もその版を送信できます。指定しない場合は最新バージョンを使います。
Java
import java.util.Map;
var receipt = sendery.prepare("[email protected]", "your-template", Map.of(
"name", "Alex",
"action_url", "https://example.com/start"
)).version(3).send();添付ファイル
ファイル名、バイト列、MIME型を指定した Attachment のリストを渡します。base64への変換はSDKが行います。
最大10ファイル、合計5 MBまで送信できます。対応形式と上限は 添付ファイルのリファレンス を参照してください。
使用例
import co.sendery.Attachment;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
var file = Files.readAllBytes(Path.of("document.pdf"));
sendery.prepare(
"[email protected]",
"your-template",
Map.of("name", "Alex", "action_url", "https://example.com/start"),
null,
"your-idempotency-key",
List.of(new Attachment("document.pdf", file, "application/pdf"))
).retry().send();