腾讯云发送短信验证码

1、在腾讯云平台中 开通短信服务

2、发送短信

2.1引用jar包

xml 复制代码
<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java-sms</artifactId>
    <version>3.1.1043</version>
</dependency>

2.2

发送短信内容:

java 复制代码
public SendResult send(SMData data) throws Exception {
		SendResult sendResult = new SendResult();
		try{
            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(new Credential(getParamValue("SecretId"), getParamValue("SecretKey")), "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {data.getReveivePhoneList()};
            req.setPhoneNumberSet(phoneNumberSet1);

            req.setSmsSdkAppId(getParamValue("SDKAppID"));
            req.setTemplateId(getParamValue("msg_template"));
            req.setSignName(getParamValue("signName"));
            //短信内容 参数个数和模板中数量匹配
            String[] param = {data.getContent()};
            req.setTemplateParamSet(param);
            
            // 返回的resp是一个SendSmsResponse的实例,与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            SendStatus status = resp.getSendStatusSet()[0];
            sendResult.setMessage(status.getMessage());
            sendResult.setSuccess(status.getCode().equalsIgnoreCase("ok"));
            
            String res = AbstractModel.toJsonString(resp);
            // 输出json格式的字符串回包
            LogHelper.Log("log_send_msg response:", res);
             
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            LogHelper.Log("log_send_msg error:", e.getMessage());
        }
		return sendResult;
	}
相关推荐
新钛云服1 小时前
如何构建一套自动化的阿里云费用报告系统
运维·阿里云·自动化·云计算
人工智能知识库3 小时前
阿里云大模型ACA题库(知识点统计)
阿里云·大模型·云计算·阿里云aca·aca
彩旗工作室5 小时前
腾讯云上调用大模型的全部入口整理(2026最新版)
人工智能·大模型·云计算·腾讯云
gaize12136 小时前
阿里云轻量适配宝塔
阿里云·云计算
云飞云共享云桌面7 小时前
东莞智能装备工厂10个solidworks共享一台服务器做装配体设计
运维·服务器·网络·云计算·电脑
easy_coder7 小时前
从 ManifestRender 到 Certificate:一次 Kubernetes 应用发布故障的深度排障实录
云原生·云计算
TG_yunshuguoji8 小时前
阿里云代理商:百炼声音复刻实战 3 步生成专属语音模型
服务器·人工智能·阿里云·云计算
安全渗透Hacker9 小时前
阿里云百炼 + VS Code + Kilo 完整实战教程
人工智能·阿里云·ai·云计算·ai编程
duoduo_sing9 小时前
反向操作!将天翼云/阿里云/腾讯云“虚拟磁盘”作为备份目标盘
阿里云·云计算·腾讯云·异地备份·松鼠备份·3-2-1备份原则
朱包林1 天前
k8s-Pod基础管理,标签管理,rc控制器及重启策略实战
linux·运维·云原生·容器·kubernetes·云计算