java發送手機短信驗證碼


第一步:首先得注冊阿里雲賬號(沒有阿里雲賬號的自己去注冊一個)

第二步:開通服務 

         1. 搜索短信服務

       
 
 

        2.添加簽名和模板(需要提交審核,審核通過后才可以使用,一般幾分鍾就可以出審核結果了)

           

 

 

第三步:編寫測試代碼

      1.打開幫助文檔,找到SDK參考

      

     需要用什么語言寫就點擊安裝什么的SDK,就行了

     

 

   我選擇環境是推薦的是添加maven依賴

 java代碼(復制調試代碼進行修改就可以實現了)

package com.liusha.sendsms;
        import com.alibaba.fastjson.JSONObject;
        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;
        import org.junit.jupiter.api.Test;
        import org.springframework.boot.test.context.SpringBootTest;

        import java.util.HashMap;

@SpringBootTest
class SendsmsApplicationTests {

    @Test
    void contextLoads() {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "填寫你的AccessKey ID", "填寫你的AccessKey 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");    //我選擇sendSms
        //自定義的參數(手機號,驗證碼,簽名,模板)
        request.putQueryParameter("PhoneNumbers", "發送到的手機號碼");
        request.putQueryParameter("SignName", "你的簽名名稱");
        request.putQueryParameter("TemplateCode", "你的模版CODE");
        //構建一個短信驗證碼(一般都是傳進來的隨機數,我這里測試直接寫死)
        HashMap<String, Object> map = new HashMap<>();
        map.put("code",5588);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            //輸出響應是否成功
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

}  
運行結果

 

 

        

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM