Send SMS via Tencent Cloud API with Spring Boot
355 words
2 minutes
Send SMS via Tencent Cloud API with Spring Boot
Send SMS via Tencent Cloud API with Spring Boot
Step 1: Apply for SMS Service
Open the Tencent Cloud console, apply for an SMS application, signature, and template.
Step 2: API Debugging
You can quickly generate calling code on the Tencent Cloud API Explorer page.
Add the Dependency
<dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java</artifactId> <version>3.1.838</version></dependency>Raw Calling Code
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()); } }}Example response:
{ "SendStatusSet": [ { "SerialNo": "3369:294906593316931104718082751", "PhoneNumber": "your-phone-number", "Fee": 1, "Code": "Ok", "Message": "send success", "IsoCode": "CN" } ], "RequestId": "69aceba9-a43d-434c-9021-e7320ef52d49"}Step 3: Wrap as a Spring Boot Utility
1. Configuration Properties
myapp: secretId: your-secret-id secretKey: your-secret-key smsSdkAppId: your-sdk-app-id signName: your-sign-name templateId: your-template-id2. Configuration Binding Class
@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}Note: Don’t make set methods
static, or@ConfigurationPropertieswon’t inject values properly.
3. SMS Utility Class
@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. API Endpoint
@RestControllerpublic class SmsController { @GetMapping("/sms") public String sendSms() { SmsUtil.sendVerificationCode("your-phone", "1234"); return "ok"; }}Summary
The evolution: API debugging (raw SDK) → externalized configuration (ConfigurationProperties) → static utility class (@PostConstruct initialization). In daily development, the wrapped utility class approach is recommended to avoid duplicating SDK calling logic across business code.
Support & Share
If this article helped you, please share or support!
Send SMS via Tencent Cloud API with Spring Boot
https://blog.zinzin.cc/posts/springboot-tencent-sms-en/Related PostsSmart
1
SpringBoot Email Sending with @Async Optimization
JavaIntegrate email sending in Spring Boot, implement async email with @Async, and troubleshoot common @Async pitfalls.
2
Request Wildcard SSL Certificates with acme.sh
DevOpsGet free Let's Encrypt wildcard certificates via acme.sh + Cloudflare DNS API with automatic renewal.
3
Deploy a Nacos Cluster with Docker
DevOpsA complete guide to deploying a 3-node Nacos 2.2 cluster with Docker, using MySQL for persistence and Nginx for load balancing.
4
Switch Java (JDK) Versions Freely with Scoop
Dev ToolsInstall multiple JDK versions on Windows using Scoop package manager and switch between them with a single command — no more manual environment variable tweaking.
5
2025 Year in Review
Annual ReviewReflections on 2025 — covering work, learning, health, books, games, anime, and travel.
Random PostsRandom













