腾讯短信验证码发送

亲测可用,需要先申请短信签名喝模板,模板的参数一定要设


/*

*腾讯短信验证码发送*/

  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();

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容