1. 程式人生 > >java接入阿里雲簡訊sdk 程式碼

java接入阿里雲簡訊sdk 程式碼

前言
程式碼

兩種傳送方式:getAcsResponse和doAction介面,這裡演示最簡單的getAcsResponse。

/**
     * 
     * @param toPhone 要傳送的手機號碼
     * @param code 驗證碼
     * @return
     */
    public String getAcsResponse(String toPhone, String code) {
        SingleSendSmsResponse httpResponse = new SingleSendSmsResponse();
        String result = ""
; try { IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKey, accessSecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Sms","sms.aliyuncs.com"); IAcsClient client = new DefaultAcsClient(profile); SingleSendSmsRequest request = new
SingleSendSmsRequest(); request.setSignName(sign);// 控制檯建立的簽名名稱 request.setTemplateCode(templateCode);// 控制檯建立的模板CODE JSONObject jsonCode = new JSONObject(); jsonCode.accumulate("code", code); request.setParamString(jsonCode.toString()); request.setRecNum(toPhone); request.setAcceptFormat(FormatType.JSON); // 格式為json
httpResponse = client.getAcsResponse(request); logger.info("send msgcode response: {} ", httpResponse.toString()); result = ConstUtils.ERROR_0; } catch (ClientException e) { e.printStackTrace();// //InvalidSignName.Malformed : The specified// sign name is wrongly formed. logger.error("send msgcode error :{}", e.getMessage()); result = e.getErrCode(); } return result; }