<dependency>
<groupId>com.github.qcloudsms</groupId>
<artifactId>qcloudsms</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<!--阿里短信验证-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
/*
*腾讯短信验证码发送*/
public Map sends(String mobile)throws Exception{
Map map =new HashMap();
int i =1;
// 短信应用SDK AppID 1400开头
int appid =your appid;
// 短信应用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();
}