sendcloud4jSendCloud 的 Java 開發(fā)包
sendcloud4j 是 SendCloud 郵件服務的 Java 語言封裝包。
特點:
-
支持 郵箱API v2 普通發(fā)送和模板發(fā)送
-
支持批量發(fā)送(模板批量變量替換)
-
支持添加附件發(fā)送
Maven
<dependency> <groupId>io.jstack</groupId> <artifactId>sendcloud4j</artifactId> <version>0.0.4</version> <dependency>
Gradle
compile 'io.jstack:sendcloud4j:0.0.4'
示例代碼:
-
初始化 API,通過 SendCloud 后臺獲取 apiUser 和 apiKey,創(chuàng)建
SendCloud實例private String apiUser = "testApiUser"; private String apiKey = "testApiKey"; SendCloud webapi = SendCloud.createWebApi(apiUser, apiKey);
-
創(chuàng)建郵件實例,支持普通郵件和模板郵件。
普通郵件,郵件內容支持 HTML 或文本:
Email email = Email.general() .from("[email protected]") .fromName("JStack Support") .html("<b>Hello World!</b>") // or .plain() .subject("mail title") .attachment(new File("att.png")) // 添加附件 (File or byte[]) .to("[email protected]");模塊郵件,使用
Substitution.sub()替換變量值:Email email = Email.template("template_order_customer") .from("[email protected]") .fromName("JStack Support") .substitutionVars(Substitution.sub() // 模板變量替換 .set("product", "iPhone 6S") .set("name", "denger")) .attachment(new File("att.png")) // 添加附件 (File or byte[]) .to("[email protected]"); -
執(zhí)行發(fā)送
Result result = webapi.mail().send(email);
-
處理發(fā)送結果
result.isSuccess(); //API 請求是否成功 result.getStatusCode(); //API 返回碼 result.getMessage(); //API 返回碼的中文解釋
評論
圖片
表情
