SpringBoot 调用腾讯云 API 发送短信
454 字
2 分钟
SpringBoot 调用腾讯云 API 发送短信
SpringBoot 调用腾讯云 API 发送短信
第一步:申请短信服务
打开腾讯云控制台,申请短信应用、签名与模板。
第二步:API 调试
在 腾讯云 API Explorer 页面可以快速生成调用代码。
导入依赖
<dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java</artifactId> <version>3.1.838</version></dependency>原始调用代码
public class SendSms { public static void main(String[] args) { try { Credential cred = new Credential("SecretId", "SecretKey"); HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("sms.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile);
SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);
SendSmsRequest req = new SendSmsRequest(); String[] phoneNumberSet1 = {"your-phone-number"}; req.setPhoneNumberSet(phoneNumberSet1); req.setSmsSdkAppId("1400849908"); req.setSignName("your-sign-name"); req.setTemplateId("1907520");
String[] templateParamSet1 = {"6666"}; req.setTemplateParamSet(templateParamSet1);
SendSmsResponse resp = client.SendSms(req); System.out.println(SendSmsResponse.toJsonString(resp)); } catch (TencentCloudSDKException e) { System.out.println(e.toString()); } }}返回结果示例:
{ "SendStatusSet": [ { "SerialNo": "3369:294906593316931104718082751", "PhoneNumber": "your-phone-number", "Fee": 1, "Code": "Ok", "Message": "send success", "IsoCode": "CN" } ], "RequestId": "69aceba9-a43d-434c-9021-e7320ef52d49"}第三步:封装为 Spring Boot 工具类
1. 配置属性
myapp: secretId: your-secret-id secretKey: your-secret-key smsSdkAppId: your-sdk-app-id signName: your-sign-name templateId: your-template-id2. 配置属性绑定类
@Component@ConfigurationProperties(prefix = "myapp")public class MyAppProperties { private String secretId; private String secretKey; private String smsSdkAppId; private String signName; private String templateId;
// getters and setters}注意:set 方法不要写成 static 的,否则
@ConfigurationProperties无法正常注入。
3. 发送短信工具类
@Component@Slf4jpublic class SmsUtil { private static SmsClient client = null;
@PostConstruct public void init() { client = createClient( MyAppProperties.getSecretId(), MyAppProperties.getSecretKey() ); }
public static void sendVerificationCode(String phone, String code) { SendSmsRequest req = new SendSmsRequest(); req.setPhoneNumberSet(new String[]{phone}); req.setSmsSdkAppId(MyAppProperties.getSmsSdkAppId()); req.setSignName(MyAppProperties.getSignName()); req.setTemplateId(MyAppProperties.getTemplateId()); req.setTemplateParamSet(new String[]{code});
try { SendSmsResponse resp = client.SendSms(req); log.info("SMS send result: {}", SendSmsResponse.toJsonString(resp)); } catch (TencentCloudSDKException e) { log.error("Failed to send SMS", e); throw new RuntimeException(e); } }
private static SmsClient createClient(String secretId, String secretKey) { Credential cred = new Credential(secretId, secretKey); HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("sms.tencentcloudapi.com"); ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); return new SmsClient(cred, "ap-beijing", clientProfile); }}4. 对外接口
@RestControllerpublic class SmsController { @GetMapping("/sms") public String sendSms() { SmsUtil.sendVerificationCode("your-phone", "1234"); return "ok"; }}总结
整个演进过程:API 调试(原生 SDK)→ 配置外置(ConfigurationProperties)→ 静态工具类封装(@PostConstruct 初始化)。日常开发中推荐直接使用封装后的工具类方式,避免在业务代码中重复编写 SDK 调用逻辑。
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
SpringBoot 调用腾讯云 API 发送短信
https://blog.zinzin.cc/posts/springboot-tencent-sms/相关文章智能推荐
1
SpringBoot 邮件发送与异步优化
JavaSpring Boot 集成邮件发送,以及使用 @Async 实现异步邮件发送,并附 @Async 失效的常见原因排查清单。
2
2025 年终总结
年度总结2025 年的回顾与思考,涵盖工作、学习、身体、书评、游戏、番剧与旅行。
3
使用 acme.sh 申请泛域名 SSL 证书
Linux运维与部署通过 acme.sh + Cloudflare DNS API 免费申请 Let's Encrypt 泛域名证书,并自动续期。
4
Docker 镜像与容器导入导出命令详解
Linux运维与部署梳理 docker commit/export/import/save/load 的区别与适用场景,帮你选对正确的命令。
5
使用 Docker 完成 Nacos 集群部署
Linux运维与部署基于 Docker 部署 Nacos 2.2 三节点集群,搭配 MySQL 持久化与 Nginx 负载均衡的完整实践。
随机文章随机推荐













