How to Send SMS Verification Codes with Alibaba Cloud’s SendSms API
This article explains the prerequisites, usage steps, and Java code for sending single SMS verification messages using Alibaba Cloud's SendSms service, including notes on batch limits and signature handling.
Introduction
Anyone who has used a mobile app has encountered the SMS verification code feature, and developers often need to implement it.
Alibaba Cloud SMS Service – SendSms
The article introduces Alibaba Cloud’s SMS service and explains that the SendSms API is used for sending single SMS messages, with limited batch support (up to 1000 numbers, with possible delay).
Prerequisites
Before sending, you must apply for an SMS signature and template and ensure they have passed review.
Usage Instructions
Single‑message sending uses the SendSms interface; for different signatures or templates per number, the SendBatchSms interface is required.
If the verification‑code signature and the generic signature share the same name, the system defaults to the generic signature.
Code Example
public ApiResult sendMsg(String mobile) throws Exception {
String randomNumber = RandomUtils.getRandomNumber(6);
SendSmsRequest sendSmsRequest = new SendSmsRequest();
sendSmsRequest.setPhoneNumbers(mobile);
sendSmsRequest.setSignName("你的签名");
sendSmsRequest.setTemplateCode("你的模板代码");
String json = "{'code':'" + randomNumber + "'}";
sendSmsRequest.setTemplateParam(json);
SendSmsResponse resp = AliSms.sendSms(sendSmsRequest, "你的acckey", "你的acckey对应秘钥串");
if (resp.getBody().getCode().equalsIgnoreCase("ok")){
String key = mobile + "_code";
redisService.set(key,randomNumber,5L, TimeUnit.MINUTES);
String o = (String)redisService.get(key);
System.out.println(o);
System.out.println(resp.toString());
}
com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(resp)));
return new ApiResult();
}The code demonstrates generating a 6‑digit random code, setting the phone number, signature, template, and sending the request, then storing the code in Redis for five minutes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Coder Trainee
Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
