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-id

2. 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 @ConfigurationProperties won’t inject values properly.

3. SMS Utility Class#

@Component
@Slf4j
public 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#

@RestController
public 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!

Sponsor
Send SMS via Tencent Cloud API with Spring Boot
https://blog.zinzin.cc/posts/springboot-tencent-sms-en/
Author
gzdyj
Published at
2026-07-23
Profile Image of the Author
gzdyj
Code, life, and everything in between.
Announcement
Welcome to my blog! This is a sample announcement.
Categories
Tags
Latest Moments
Site Statistics
Posts
8
Categories
4
Tags
27
Total Words
13,116
Running Days
0 days
Last Activity
0 days ago
Site Info
Build Platform
GitHub Actions
Blog Version
Firefly v6.14.5
License
CC BY-NC-SA 4.0