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

相关推荐
独自破碎E7 小时前
【回溯】二叉树的所有路径
android·java
风景的人生7 小时前
application/x-www-form-urlencoded
java·mvc
sheji34167 小时前
【开题答辩全过程】以 基于Java的流浪猫救济中心系统的设计与实现为例,包含答辩的问题和答案
java·开发语言
毕设源码-郭学长7 小时前
【开题答辩全过程】以 高校选修课管理系统的设计与实现为例,包含答辩的问题和答案
java
岱宗夫up7 小时前
Scrapy框架实战教程(上):从入门到实战,搭建你的第一个专业爬虫
爬虫·python·scrapy
爱吃面条的猿7 小时前
FontMetrics 字体属性类 java
java
Bruk.Liu7 小时前
(LangChain实战4):LangChain消息模版PromptTemplate
人工智能·python·langchain
SunnyRivers7 小时前
Asyncio 提速秘籍:用 run_in_executor 与 to_thread 巧解同步阻塞难题
python·asyncio·to_thread·run_in_executor
蒹葭玉树7 小时前
【C++上岸】C++常见面试题目--操作系统篇(第二十九期)
java·c++·面试
小小仙。7 小时前
IT自学第二十天
java·开发语言