其實官方api中已經寫的很明白了,但是為了方便以后查看,還是搬一下吧。
之前都是用的互億無線短信平台,最近幾個項目才轉的阿里雲。 沒啥難度的,僅是記錄下。
在我的博客查看:zjblog
首先結合項目需求在阿里雲申請幾個模板,簽名也需要申請下。
調用接口方法
public static String sendSms(String phone, String templateCode, String otherparams) {
DefaultProfile profile =
DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", "微信平台移動辦公");
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", otherparams);
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
return response.getData();
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return null;
}
調用發送短信方法
JSONObject jsonParam = new JSONObject();
jsonParam.put("realname", realname);
jsonParam.put("type", typename);
SendSms.sendSms("1777*****72", "SMS_183793510", jsonParam.toString());//SMS_183793510為對應的模板code