腾讯云发送短信验证码

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;
	}
相关推荐
Johny_Zhao5 小时前
基于 Docker 的 LLaMA-Factory 全流程部署指南
linux·网络·网络安全·信息安全·kubernetes·云计算·containerd·yum源·系统运维·llama-factory
真上帝的左手7 小时前
20. 云计算-云服务模型
云计算
nshkfhwr9 小时前
什么是微服务
微服务·云原生·架构·云计算·集群
荣光波比14 小时前
Linux(十一)——LVM磁盘配额整理
linux·运维·云计算
牛奶咖啡1316 小时前
云计算核心技术之容器技术
云计算·容器技术·容器引擎·容器的主要应用场景·docker核心技术·容器编排工具·k8s的运行架构和重要概念
XINVRY-FPGA21 小时前
10CL016YF484C8G Altera FPGA Cyclone
嵌入式硬件·网络协议·fpga开发·云计算·硬件工程·信息与通信·fpga
AKAMAI1 天前
AI需要防火墙,云计算需要重新构想
人工智能·云原生·云计算
是乐谷3 天前
阿里云杭州 AI 产品法务岗位信息分享(2025 年 8 月)
java·人工智能·阿里云·面试·职场和发展·机器人·云计算
青岛佰优联创新科技有限公司3 天前
移动板房的网络化建设
服务器·人工智能·云计算·智慧城市
夕阳与风馨3 天前
三分钟搞懂云计算三大模型:SaaS、PaaS、IaaS 是怎么在业务中“各司其职”的?
后端·云计算