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

相关推荐
考虑考虑9 分钟前
JDK9中的dropWhile
java·后端·java ee
想躺平的咸鱼干17 分钟前
Volatile解决指令重排和单例模式
java·开发语言·单例模式·线程·并发编程
hqxstudying1 小时前
java依赖注入方法
java·spring·log4j·ioc·依赖
·云扬·1 小时前
【Java源码阅读系列37】深度解读Java BufferedReader 源码
java·开发语言
巴里巴气2 小时前
selenium基础知识 和 模拟登录selenium版本
爬虫·python·selenium·爬虫模拟登录
19892 小时前
【零基础学AI】第26讲:循环神经网络(RNN)与LSTM - 文本生成
人工智能·python·rnn·神经网络·机器学习·tensorflow·lstm
JavaEdge在掘金2 小时前
Redis 数据倾斜?别慌!从成因到解决方案,一文帮你搞定
python
ansurfen2 小时前
我的第一个AI项目:从零搭建RAG知识库的踩坑之旅
python·llm
Bug退退退1232 小时前
RabbitMQ 高级特性之重试机制
java·分布式·spring·rabbitmq
小皮侠2 小时前
nginx的使用
java·运维·服务器·前端·git·nginx·github