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即可

相关推荐
独自破碎E6 小时前
BISHI43 讨厌鬼进货
android·java·开发语言
深蓝海拓6 小时前
PySide6的QTimeLine详解
笔记·python·qt·学习·pyqt
MX_93596 小时前
Spring xml 方式整合第三方框架总结加案例
xml·java·spring
没有bug.的程序员6 小时前
服务网格(Istio)与传统微服务深度对垒:流量治理内核、代码侵入性博弈与运维收益实战指南
java·运维·微服务·istio·流量治理内核·代码侵入性
该叫啥6 小时前
Spring Bean 生命周期
java·spring·servlet
纯.Pure_Jin(g)6 小时前
【Python练习四】Python 算法与进阶特性实战:数组、序列化与位运算专项练习(3道经典练习带你巩固基础——看完包会)
开发语言·vscode·python
星火开发设计7 小时前
虚析构函数:解决子类对象的内存泄漏
java·开发语言·前端·c++·学习·算法·知识
好大的月亮7 小时前
中值法排序及LexoRank排序算法简述
java·算法·排序算法
TongSearch7 小时前
Tongsearch分片的分配、迁移与生命周期管理
java·服务器·数据库·elasticsearch·tongsearch
龙山云仓7 小时前
No152:AI中国故事-对话祖冲之——圆周率与AI精度:数学直觉与极限探索
大数据·开发语言·人工智能·python·机器学习