前提
由于我申请了7次阿里云的短信签名还是被拒了,一直要求是已上架的网站(我上架了,还要做什么短信???上架个不要短信的,我还申请干啥)就用了腾讯云,不得不说,2分钟审核完成,还送了200条短信测试,这波,这波 牛 X(需要有备案的网站,不管在哪里备案的,我的是在阿里云备案的,一样过了)
正文(准备)
直接进去按照这个步骤来,我是过了,所以都是完成,个人只需要备案的网站就行了
创建一个应用(需要有appid 默认是创建好的)
还需要一个secretId和key
准备完成
上代码
//springboot依赖省略
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.87</version>
</dependency>
public Boolean sendTX(String phone, String pram) {
try {
Credential cred = new Credential("secretId", "secretKey");
HttpProfile httpProfile = new HttpProfile();
httpProfile.setConnTimeout(60);
httpProfile.setEndpoint("sms.tencentcloudapi.com");
/* 非必要步骤:
* 实例化一个客户端配置对象,可以指定超时时间等配置 */
ClientProfile clientProfile = new ClientProfile();
/* SDK 默认用 TC3-HMAC-SHA256 进行签名
* 非必要请不要修改该字段 */
clientProfile.setSignMethod("HmacSHA256");
clientProfile.setHttpProfile(httpProfile);
/* 实例化 SMS 的 client 对象
* 第二个参数是地域信息,可以直接填写字符串 ap-guangzhou,或者引用预设的常量 */
SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
SendSmsRequest req = new SendSmsRequest();
req.setSmsSdkAppid("SdkID");
req.setSign("你的签名名字");
req.setTemplateID("你的模板id");
//特别注意腾讯云的手机号需要加+86表示
String[] phoneNumbers = {"+86"+phone};
req.setPhoneNumberSet(phoneNumbers);
/* 模板参数:这个就是你的验证码,自己用随机数生成一个 */
String[] templateParams = {pram};
req.setTemplateParamSet(templateParams);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
补充
有些为固定值不要去修改,手机号和验证码都是数组形式,验证码的设置对应的是你的模板里的参数
这是基础教程,验证码的过期等教程以后有时间再写