SpringBoot集成阿里雲短信

1.在主賬號下申請子賬號 拿到AccessKey ID和SECRET

 

2.默認主賬號已經實名及開通短信服務情況下,對子賬號授權

 

 

3.用主賬號登錄,設置簽名及模板

 

3.1設置簽名管理,簽名名稱即爲項目中SignName所對應的值

 

 

3.2設置模板管理,模板Code值即爲編碼中TemplateCode所對應的值

OpenAPI Explorer中輸入PhoneNumbers(手機號)、SignName、TemplateCode、TemplateParam(驗證碼)即可,如下圖所示:

 

測試代碼如下:

/**

* @author: yingch

* @date: 2020/6/16 14:51

* @description:

*/



import com.aliyuncs.CommonRequest;

import com.aliyuncs.CommonResponse;

import com.aliyuncs.DefaultAcsClient;

import com.aliyuncs.IAcsClient;

import com.aliyuncs.exceptions.ClientException;

import com.aliyuncs.exceptions.ServerException;

import com.aliyuncs.http.MethodType;

import com.aliyuncs.profile.DefaultProfile;



/*pom.xml中添加依賴

<dependency>

<groupId>com.aliyun</groupId>

<artifactId>aliyun-java-sdk-core</artifactId>

<version>4.5.1</version>

</dependency>

*/



public class SMSTest {



    public static void main(String[] args) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "ID值", "Secret值");//參數一默認值,參數二賬號AccessKey ID,參數三SECRET

        IAcsClient client = new DefaultAcsClient(profile);



        CommonRequest request = new CommonRequest();

        request.setSysMethod(MethodType.POST);

        request.setSysDomain("dysmsapi.aliyuncs.com");

        request.setSysVersion("2017-05-25");

        request.setSysAction("SendSms");

        request.putQueryParameter("RegionId", "cn-hangzhou");

        request.putQueryParameter("PhoneNumbers", "手機號");//變量手機號

        request.putQueryParameter("SignName", "簽名名稱");

        request.putQueryParameter("TemplateCode", "模板名稱");

        request.putQueryParameter("TemplateParam", "{\"code\":\"4321\"}");//變量驗證碼

        try {

            CommonResponse response = client.getCommonResponse(request);

            System.out.println(response.getData());

        } catch (ServerException e) {

            e.printStackTrace();

        } catch (ClientException e) {

            e.printStackTrace();

        }

    }





}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章