阿里短信產(chǎn)品,發(fā)送短信的工具類

公司某個(gè)線上項(xiàng)目的月短信量

經(jīng)常使用阿里產(chǎn)品的同學(xué)對(duì)于阿里的產(chǎn)品使用起來應(yīng)該是輕車熟路了。我這里就不過多的贅述了。

倉庫坐標(biāo)
<!--阿里云短信產(chǎn)品-->
    <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.2.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-dysmsapi -->
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
        <version>1.1.0</version>
    </dependency>
工具類書寫
package cn.lpck.bxgg.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * @Author:LiuPu
 * @Date:2018/11/29 23:41
 * @Description:
 * @Version 1.011
 */
public class SendMessageUtil {

    //產(chǎn)品名稱:云通信短信API產(chǎn)品,開發(fā)者無需替換
    static final String product = "Dysmsapi";
    //產(chǎn)品域名,開發(fā)者無需替換
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此處需要替換成開發(fā)者自己的AK(在阿里云訪問控制臺(tái)尋找)
    static final String accessKeyId = "Xxxxxxxxxxxxxxxx";
    static final String accessKeySecret = "wBnxxxxxxxxxxxxxxxxxx";

    //發(fā)短信
    public static SendSmsResponse sendSms(String telephone,String code) throws ClientException {

        //可自助調(diào)整超時(shí)時(shí)間
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暫不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //組裝請(qǐng)求對(duì)象-具體描述見控制臺(tái)-文檔部分內(nèi)容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待發(fā)送手機(jī)號(hào)
        request.setPhoneNumbers(telephone);
        //必填:短信簽名-可在短信控制臺(tái)中找到
        request.setSignName("短信簽名xxxxx");
        //必填:短信模板-可在短信控制臺(tái)中找到
        request.setTemplateCode("短信模板xxxxxxx");
        //可選:模板中的變量替換JSON串,如模板內(nèi)容為"親愛的${name},您的驗(yàn)證碼為${code}"時(shí),此處的值為
        request.setTemplateParam("{\"number\":\""+code+"\"}");


        //hint 此處可能會(huì)拋出異常,注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;
    }
}

控制臺(tái)模板的管理
控制臺(tái)詳細(xì)信息

短信模板詳情

創(chuàng)建模板的時(shí)候我們要在會(huì)變更信息的地方編輯我們的站位屬性,這個(gè)我在工具類的注釋上也寫的很明確了(其實(shí)注釋上的文字都是阿里云官方提供的文檔中的一些文字,我只是原封不動(dòng)的拿了過來而已)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容