本文主要是介绍使用腾讯云sms实现短信验证功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用腾讯云sms实现短信验证功能
腾讯云短信免费试用:https://console.cloud.tencent.com/smsv2
腾讯云短信功能注册完成,并通过审批。就可以得到以下参数。
SecretId
SecretKey
先创建自己的签名
审核通过记住签名的ID以及内容
然后申请短信模板
审核通过后获取模板的ID
获取应用AppID
然后根据语言选择对于的SDK(这里主要讲Java的方法)
JavaSDK地址:https://cloud.tencent.com/document/product/382/43194
注意这里的版本号只是示例,需要点击最新的版本号查看最新版本
短信模板的参数有两个就再添加一个
最后附上我的发送短信的方法
package com.ruoyi.common.sms;import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;import com.tencentcloudapi.sms.v20190711.SmsClient;import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;import java.util.Random;public class SendSms {private static final String secretId = "A**************R";private static final String secretKey = "j************O";private static final String TemplateID = "5****6";private static final String Sign = "上***";//时间参数private static final String SmsTime = "1";private static final String SmsSdkAppid = "1********9";public String Send(String SendPhone) {String TemplateParamSet = null;try {Credential cred = new Credential(secretId, secretKey);HttpProfile httpProfile = new HttpProfile();httpProfile.setEndpoint("sms.tencentcloudapi.com");ClientProfile clientProfile = new ClientProfile();clientProfile.setHttpProfile(httpProfile);SmsClient client = new SmsClient(cred, "ap-shanghai", clientProfile);//验证码int yzm = random();TemplateParamSet = Integer.toString(yzm);//模板参数String[] templateParams = {TemplateParamSet,SmsTime};String[] phoneNumbers = {"+86"+SendPhone};SendSmsRequest req = new SendSmsRequest();req.setPhoneNumberSet(phoneNumbers);req.setSmsSdkAppid(SmsSdkAppid);req.setSign(Sign);req.setTemplateID(TemplateID);req.setTemplateParamSet(templateParams);SendSmsResponse resp = client.SendSms(req);} catch (TencentCloudSDKException e) {System.out.println(e.toString());}return TemplateParamSet;}/*生成6位验证码*/private static int random() {Random r = new Random();StringBuffer sb = new StringBuffer();int[] c = new int[6];for (int i = 0; i < 6; i++) {c[i] = r.nextInt(9) + 1;sb.append(c[i]);}return Integer.parseInt(sb.toString());}}
这篇关于使用腾讯云sms实现短信验证功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!