1. 程式人生 > 其它 >快速上手簡訊服務 —— 榛子云簡訊介面

快速上手簡訊服務 —— 榛子云簡訊介面

前言

  大家第一次使用簡訊服務應該都是用於學習,而阿里雲、騰訊雲...等大廠的雲服務稽核比較嚴格,對於學習者大概率無法通過,於是在此提供一個無需稽核即可使用的簡訊服務介面用於學習 —— 榛子云簡訊服務

使用步驟

1.註冊賬號

2.建立簡訊模板

注意:建立簡訊模板是人工稽核,不會寫可參考騰訊雲、阿里雲簡訊模板,由於是小公司,稽核較慢,建議儘早建立。建立完成後會生成一個模板 ID(templateId) 這就是本模板的標識,通過 templateId 使用該模板傳送簡訊。

3.檢視 AppId 和 AppSecret

AppId 和 AppSecret 用於標識傳送使用者,注意不要洩露,防止被非法使用。

4.封裝簡訊服務

  • 匯入所需要的依賴
<dependency>
  <groupId>com.zhenzikj</groupId>
  <artifactId>zhenzisms</artifactId>
  <version>2.0.2</version>
</dependency>
  • 封裝 Sms 工具類
public class SendSms {
    public static void send(String phoneNumber, String code) {
        // 使用自己的 AppId 和 AppSecret
        ZhenziSmsClient client = new ZhenziSmsClient("https://sms_developer.zhenzikj.com", "XXXXXX", "XXXX-XXX-XXXX");
        Map<String, Object> params = new HashMap<>();
        params.put("number", phoneNumber);
        // 修改為自己的templateId
        params.put("templateId", "XXXX");
        String[] templateParams = new String[1];
        templateParams[0] = code;
        params.put("templateParams", templateParams);
        try {
            String result = client.send(params);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

5.測試使用簡訊服務