-
在腾讯云创建短信签名#

-
短信签名审核通过后创建短信模板#


-
短信发送SDK(java)文档,将com.tencentcloudapi包导入#
Copy
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.423</version>
</dependency>
-
在配置文件配置相应的参数信息#
Copy
tencent:
sms:
keyId: 123
keysecret: 123
smsSdkAppId: 123
signName: "我的技术记录"
templateId: 123
-
腾讯云短信发送在线Api调试(点击查询对应参数信息)#

-
新建ConstantSmsUtils#
Copy
@Component
public class ConstantSmsUtils implements InitializingBean {
@Value("${tencent.sms.keyId}")
private String secretID ;
@Value("${tencent.sms.keysecret}")
private String secretKey ;
@Value("${tencent.sms.smsSdkAppId}")
private String smsSdkAppID ;
@Value("${tencent.sms.signName}")
private String signName ;
@Value("${tencent.sms.templateId}")
private String templateID ;
public static String SECRET_ID;
public static String SECRET_KEY;
public static String SMSSDKAPP_ID;
public static String SIGN_NAME;
public static String TEMPLATE_ID;
@Override
public void afterPropertiesSet() throws Exception {
SECRET_ID = secretID;
SECRET_KEY = secretKey;
SMSSDKAPP_ID = smsSdkAppID;
SIGN_NAME = signName;
TEMPLATE_ID = templateID;
}
}
-
新建SmsService和SmsServiceImpl#
Copy
public interface SmsService {
Boolean send (String phone);
}
Copy
@Service
public class SmsServiceImpl implements SmsService {
@Override
public Boolean send(String phone) {
if (StringUtils.isEmpty(phone)){
return false;
}
try{
Credential cred = new Credential(ConstantSmsUtils.SECRET_ID, ConstantSmsUtils.SECRET_KEY);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("sms.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
SendSmsRequest req = new SendSmsRequest();
req.setSmsSdkAppid(ConstantSmsUtils.SMSSDKAPP_ID);
req.setSmsSdkAppid(ConstantSmsUtils.SMSSDKAPP_ID);
req.setSign(ConstantSmsUtils.SIGN_NAME);
req.setTemplateID(ConstantSmsUtils.TEMPLATE_ID);
String[] phoneNumberSet1 = {"+86"+phone};
req.setPhoneNumberSet(phoneNumberSet1);
String[] templateParamSet1 = {"123","1"};
req.setTemplateParamSet(templateParamSet1);
SendSmsResponse resp = client.SendSms(req);
System.out.println("resp"+resp);
System.out.println(SendSmsResponse.toJsonString(resp));
return true;
} catch (TencentCloudSDKException e) {
e.printStackTrace();
return false;
}
}
}
-
编写接口进行测试#
Copy
@RestController
@RequestMapping("/edusms/sms")
public class SmsController {
@Autowired
private SmsService smsService;
@Resource
private RedisUtils redisUtils;
@GetMapping("/send/{phone}")
public String sendSms(@PathVariable String phone){
boolean isSend = smsService.send(phone);
if (isSend){
return "success";
}else {
return "error";
}
}
}

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构