亲测可用,需要先申请短信签名喝模板,模板的参数一定要设
/*
*腾讯短信验证码发送*/
public Map sends(String mobile)throws Exception{
Map map =new HashMap();
int i =1;
// 短信应用SDK AppID
int appid =your appid;// 1400开头
// 短信应用SDK AppKey
String appkey =yourappkey;
// 需要发送短信的手机号码
String[] phoneNumbers = { mobile };
String randomCode = getRandomCode();
try {
SmsSingleSender ssender =new SmsSingleSender(appid, appkey);
SmsSingleSenderResult result = ssender.send(0,"86", phoneNumbers[0], randomCode+"为您的登录验证码,请于5分钟内填写。如非本人操作,请忽略本短信。","","");
i = result.result;
map.put("result",i);
map.put("randomCode",randomCode);
}catch (HTTPException e) {// HTTP响应码错误
e.printStackTrace();
}catch (JSONException e) {// json解析错误
e.printStackTrace();
}catch (IOException e) {// 网络IO错误
e.printStackTrace();
}catch (org.json.JSONException e) {
e.printStackTrace();
}catch (com.github.qcloudsms.httpclient.HTTPException e) {
e.printStackTrace();
}
return map;
}
/**
* 随机生成6位验证码
* @return
*/
private String getRandomCode(){
Random random =new Random();
StringBuffer result=new StringBuffer();
for (int i=0;i<6;i++){
result.append(random.nextInt(10));
}
return result.toString();
}