1. 程式人生 > 其它 >非maven專案使用阿里雲簡訊服務API

非maven專案使用阿里雲簡訊服務API

不說廢話,先上程式碼

單發
/**
     * 單發簡訊
     * @param phone  手機號
     * @param code   模板code
     * @return
     * @throws ClientException
     */
    public static void  sendSms(String phone,String params) throws ClientException {
    	
    	//產品名稱:雲通訊簡訊API產品,開發者無需替換
        final String product = "Dysmsapi";
        //產品域名,開發者無需替換
        final String domain = "dysmsapi.aliyuncs.com";

        // TODO 此處需要替換成開發者自己的AK(在阿里雲訪問控制檯尋找)    --需要修改--
        final String accessKeyId = "LTAI5tQPCzotEAPnMR5NTGhn";
        final String accessKeySecret = "T0SU6SM2p5XYoK8Uoj2zN5Y8T01vz3";


        //可自助調整超時時間 
        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);

        //組裝請求物件-具體描述見控制檯-文件部分內容
        SendSmsRequest request = new SendSmsRequest(); 
        //必填:待發送手機號    --手機號--
        request.setPhoneNumbers(phone);
        //必填:簡訊簽名-可在簡訊控制檯中找到
        request.setSignName("阿里雲簡訊測試");//
        //必填:簡訊模板唯一標識-可在簡訊控制檯中找到 
        request.setTemplateCode("SMS_154950909");
        //可選:模板中的變數替換JSON串,如模板內容為"親愛的${name},您的驗證碼為${code}"時,此處的值為
//        request.setTemplateParam("{\"code\":\"" + code + "\"}"); 
        request.setTemplateParam(params); 

        //選填-上行簡訊擴充套件碼(無特殊需求使用者請忽略此欄位)
        //request.setSmsUpExtendCode("90997");

        //可選:outId為提供給業務方擴充套件欄位,最終在簡訊回執訊息中將此值帶回給呼叫者
        request.setOutId("yourOutId");
        try {
            SendSmsResponse response = acsClient.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());  
        }     

    }
批量傳送
/**
   * 批量傳送
   * @param phones
   * @return
   * @throws ClientException
   */
 public static SendBatchSmsResponse SendBatchSms(String phones) throws ClientException {
    	
    	//初始化ascClient需要的幾個引數
    	final String product = "Dysmsapi";//簡訊API產品名稱(簡訊產品名固定,無需修改)
    	final String domain = "dysmsapi.aliyuncs.com";//簡訊API產品域名(介面地址固定,無需修改)
    	//替換成你的AK
    	final String accessKeyId = "LTAI5tQPCzotEAPnMR5NTGhn";//你的accessKeyId,參考本文件步驟2
    	final String accessKeySecret = "T0SU6SM2p5XYoK8Uoj2zN5Y8T01vz3";//你的accessKeySecret,參考本文件步驟2

    	//設定超時時間-可自行調整
    	System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
    	System.setProperty("sun.net.client.defaultReadTimeout", "10000");
    	
    	//初始化ascClient,暫時不支援多region(請勿修改)
    	IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId,accessKeySecret);
    	DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
    	IAcsClient acsClient = new DefaultAcsClient(profile); 
    	 //組裝請求物件
    	 SendBatchSmsRequest request = new SendBatchSmsRequest();
    	 //使用post提交
    	 request.setMethod(MethodType.POST);
    	 //必填:待發送手機號。支援JSON格式的批量呼叫,批量上限為100個手機號碼,批量呼叫相對於單條呼叫及時性稍有延遲,驗證碼型別的簡訊推薦使用單條呼叫的方式

    	 request.setPhoneNumberJson(phones);
    	 
    	 //必填:簡訊簽名-支援不同的號碼傳送不同的簡訊簽名
    	 request.setSignNameJson("[\"於闖的個人網站\",\"於闖的個人網站\"]");
    	 
    	 //必填:簡訊模板-可在簡訊控制檯中找到 
    	 request.setTemplateCode("SMS_264115514");
    	 //必填:模板中的變數替換JSON串,如模板內容為"親愛的${name},您的驗證碼為${code}"時,此處的值為
    	 //友情提示:如果JSON中需要帶換行符,請參照標準的JSON協議對換行符的要求,比如簡訊內容中包含\r\n的情況在JSON中需要表示成\\r\\n,否則會導致JSON在服務端解析失敗
    	 request.setTemplateParamJson("[{\"code\":\"333333\"},{\"code\":\"555553\"}]");
    	 //
    	 //可選-上行簡訊擴充套件碼(擴充套件碼欄位控制在7位或以下,無特殊需求使用者請忽略此欄位)
    	 //request.setSmsUpExtendCodeJson("[\"90997\",\"90998\"]");
    	
    	 //請求失敗這裡會拋ClientException異常
    	SendBatchSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
    	 
    	if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
    		//請求成功
    		System.out.println("傳送成功!");
    	}
    	return sendSmsResponse;
    }
說明

1,兩個方法的傳入引數,返回引數啥的,自己可以按照需求刪改
2,簽名和模板搭配確實有要求,具體可參見阿里雲文件或者諮詢線上工程師
3,這是阿里雲舊版的簡訊API,新版的我覺得比較適合maven專案,因為匯入包比較方便.
這個API只需要匯入兩個必要的jar包,還有一個關於json格式的jar包,如果需要手動傳入引數的話,就需要這個包,變成json格式傳入
三個包的連結:簡訊服務jar包

呼叫介面程式碼
//大家看懂規則,測試程式碼就可以隨心意寫了,我這裡也不完整
 public static void main(String[] args) throws ClientException, InterruptedException {
    	Gson gson = new Gson();
    	
    	//電話引數 
    	String phone="18298063645";
    	//code引數
    	Map map=new HashMap();
    	map.put("code","88888");
    	
    	String params=gson.toJson(map);
    	System.out.println(params);
		
		
    	sendSms(phone,params);

    }