使用腾讯云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());

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值