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

相关推荐
shenzhenNBA13 小时前
如何在python项目中使用日志功能?通用版本
java·开发语言·python·日志·log
毕设源码-朱学姐13 小时前
【开题答辩全过程】以 基于Vue Springboot的图书共享系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
IT·小灰灰13 小时前
Doubao-Seedream-4.5:当AI学会“版式设计思维“——设计师的七种新武器
javascript·网络·人工智能·python·深度学习·生成对抗网络·云计算
m0_7400437313 小时前
Spring和SpringBoot和SpringMVC 的关系总结
java·spring boot·spring·mvc
float_com13 小时前
【JavaWeb常见BUG】—— Lombok依赖失效问题
java·javaweb·bug分析与解决
通往曙光的路上13 小时前
异步任务la
java·开发语言
SadSunset13 小时前
(16)MyBatis执行流程分析(偏上层架构)
java·架构·mybatis
Tan_Ying_Y13 小时前
JVM内存结构,什么是栈桢?
java·jvm
木井巳13 小时前
【多线程】Thread类及常用方法
java·java-ee
秋刀鱼 ..13 小时前
第五届计算机、物联网与控制工程国际学术会议(CITCE 2025)
人工智能·python·物联网·机器人·制造·新人首发