阿里雲簡訊對接
static final String product = "Dysmsapi";
//產品域名,開發者無需替換
static final String domain = "dysmsapi.aliyuncs.com";
// TODO 此處需要替換成開發者自己的AK(在阿里雲訪問控制檯尋找)
static final String accessKeyId = ""; //需要自己申請
static final String accessKeySecret = ""; //需要自己申請
public static SendSmsResponse sendSms(String phone,String code) throws ClientException {
//可自助調整超時時間
System.setProperty("sun.net.client.defaultConnectTimeout", "5000");
System.setProperty("sun.net.client.defaultReadTimeout", "5000");
//初始化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("XXX");
//必填:簡訊模板-可在簡訊控制檯中找到
request.setTemplateCode("SMS_1403300045");
//可選:模板中的變數替換JSON串,如模板內容為"親愛的${name},您的驗證碼為${code}"時,此處的值為
request.setTemplateParam("{\"code\":\""+code+"\"}");
//選填-上行簡訊擴充套件碼(無特殊需求使用者請忽略此欄位)
//request.setSmsUpExtendCode("90997");
//可選:outId為提供給業務方擴充套件欄位,最終在簡訊回執訊息中將此值帶回給呼叫者
request.setOutId("yourOutId");
//hint 此處可能會丟擲異常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse;
}
public static void main(String[] args) throws ClientException {
SendCode.sendSms("",new RandomNum().getRandom());
System.out.println("傳送成功!");
}
public class RandomNum {
private String random =(int) ((Math.random()*9+1)*100000)+"";
public static String num;
public String code(){
return random;
}
public String getRandom() {
return random;
}
}
}