腾讯短信API对接实现代码

package com.rjnetwork.software.dev.crsd.util;


import java.io.IOException;
import java.util.Date;


import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.json.JSONObject;
import com.xiaoleilu.hutool.json.JSONUtil;
import com.xiaoleilu.hutool.util.RandomUtil;


public class TencentCloudSmsUtil {


/**appid**/
public static final Integer appid = new Integer(140009*****);

/**appkey**/
public static final String appkey = new String("d0e864b34326bfb4155d421f6ea1c476");


/** 短信模板ID,需要在短信应用中申请**/
public static final Integer templateId = new Integer(129784);

/** 签名**/
public static final String smsSign = "光速车行"; 

/**发送短信**/
public static JSONObject sendSms(String phone) {
JSONObject jsonObject = JSONUtil.createObj();//{"result":0,"errmsg":"OK","ext":"","sid":"8:QDWcB3amfNukSewXgyR20180530","fee":1}
try {
   SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
   String code = RandomUtil.randomNumbers(6);
   Date date = null;
   Date finishDate = null;
   String[] params = {code,"5"};
   SmsSingleSenderResult result = ssender.sendWithParam("86", phone,
           templateId, params, smsSign, "", "");  // 签名参数未提供或者为空时,会使用默认签名发送短信
   if(result.errMsg.equals("OK")) {
    date = new Date();//返回时间计算 返回时间是毫秒
    finishDate=DateUtil.offsetMinute(date, 5);//验证码失效时间  返回时间是毫秒
   }
   jsonObject.put("state", "ok");
   jsonObject.put("errmsg", "发送成功");
   jsonObject.put("date", date);
   jsonObject.put("finishDate", finishDate);
   jsonObject.put("code", code);
   jsonObject.put("phone", phone);
} catch (HTTPException e) {
   jsonObject.put("phone", phone);
   jsonObject.put("state", "failed");
   jsonObject.put("errmsg", "http状态码错误:"+e);
} catch (JSONException e) {
jsonObject.put("phone", phone);
jsonObject.put("state", "failed");
jsonObject.put("errmsg", "json格式化错误:"+e);
} catch (IOException e) {
jsonObject.put("phone", phone);
jsonObject.put("state", "failed");
jsonObject.put("errmsg", "网络连接错误:"+e);
}
return jsonObject;
}

public static void main(String[] args) {
System.out.println(TencentCloudSmsUtil.sendSms("135****2695"));
}

}



更多精彩内容 可以加qq群 进行交流哦!

QQ群:566421339

微信公众号:仁杰网络工作室

微信号:XHKJ_IT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值