java项目发送短信--腾讯云

1.引入依赖

java 复制代码
 <!-- 腾讯云发送短信 -->
        <dependency>
            <groupId>com.tencentcloudapi</groupId>
            <artifactId>tencentcloud-sdk-java</artifactId>
            <version>3.1.287</version>
        </dependency>

2.创建一个工具类

java 复制代码
package com.hstech.system.service.impl;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.springframework.stereotype.Service;

@Service
public class SmsProviderService {

    private static final String SECRET_ID = "YOUR_SECRET_ID"; // SecretId
    private static final String SECRET_KEY = "YOUR_SECRET_KEY"; // SecretKey
    private static final String REGION = "ap-dalian"; // 根据实际情况选择地域
    private static final String SDK_APP_ID = "创建应用时给的"; // SDK App ID
    private static final String SIGN_NAME = "YOUR_SIGN_NAME"; // 签名
    private static final String TEMPLATE_ID = "YOUR_TEMPLATE_ID"; // 模板ID

    public void sendSms(String phoneNumber, String content) {
        System.out.println("电话:"+ phoneNumber+"内容:"+content);
           /* Credential cred = new Credential(SECRET_ID, SECRET_KEY);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, REGION, clientProfile);

            SendSmsRequest req = new SendSmsRequest();

            req.setPhoneNumberSet(new String[]{phoneNumber});
            req.setSmsSdkAppId(SDK_APP_ID);
            req.setSignName(SIGN_NAME);
            req.setTemplateId(TEMPLATE_ID);
            req.setTemplateParamSet(new String[]{content}); // 假设模板只有一个占位符

            SendSmsResponse resp = client.SendSms(req);

            System.out.println(SendSmsResponse.toJsonString(resp));*/
    }
}

2.2 在实体类用运用

java 复制代码
    @Resource
    private SmsProviderService smsProviderService;

    public void handlePoliceTask() {
          int phone = 18200000000; // 手机号
          smsProviderService.sendSms(phone, "短信内容");
    }

3.注册腾讯云 ---个人账户需要实名认证后,编辑模拟的签名及id

注册好腾讯云以后点击免费的产品---搜--短信---会有一个默认的应用 取出那个id即可

相关推荐
疯狂成瘾者2 分钟前
增强型大模型代理
python
小李云雾2 分钟前
FastAPI 后端开发:文件上传 + 表单提交
开发语言·python·lua·postman·fastapi
Legend NO244 分钟前
数据资产评估风险识别、分析与管控体系建设
大数据·人工智能·python
高斯林.神犇5 分钟前
四、依赖注入.spring
java·后端·spring
hero.fei8 分钟前
在springboot中使用Resilience4j
java·spring boot·后端
llm大模型算法工程师weng9 分钟前
Python敏感词检测方案详解
开发语言·python·c#
沐苏瑶9 分钟前
Java算法之排序
java·算法·排序算法
Ricky111zzz12 分钟前
leetcode学python记录2
python·算法·leetcode·职场和发展
毕胜客源码15 分钟前
改进yolov8的香蕉成熟度检测系统,改进前后的模型指标对比,有技术文档,支持图像、视频和摄像实时检测
人工智能·python·深度学习·yolo·django
java1234_小锋15 分钟前
Java高频面试题:Redis是单线程还是多线程?
java·redis·面试