好的,以下是Springboot整合腾讯云短信服务的示例代码: 1. 首先在腾讯云短信服务控制台中创建 SMS 应用,并获取 APP ID、APP KEY 和 APP SECRET。 2. 接下来,添加依赖: “`xml <dependency> <groupId>com.github.qcloudsms</groupId> <artifactId>qcloudsms</artifactId> <version>1.0.6</version> </dependency> “` 3. 在 Springboot 配置文件中添加腾讯云短信服务的配置: “`yaml tencent: sms: app-id: <your app id> app-key: <your app key> app-secret: <your app secret> sign-name: <your sign name> “` 4. 创建短信发送工具类 SmsUtils: “`java import com.github.qcloudsms.SmsSingleSender; import com.github.qcloudsms.SmsSingleSenderResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class SmsUtils { @Value("${tencent.sms.app-id}") private int appId; @Value("${tencent.sms.app-key}") private String appKey; @Value("${tencent.sms.sign-name}") private String signName; @Autowired private SmsSingleSender sender; public void send(String phoneNumber, String templateId, String[] params) throws Exception { SmsSingleSenderResult result = sender.sendWithParam("86", phoneNumber, templateId, params, signName, "", ""); if (result.result != 0) { throw new RuntimeException(result.errMsg); } } public SmsSingleSender getSender() { return new SmsSingleSender(appId, appKey); } } “` 5. 在 Controller 中调用短信发送工具类: “`java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController public class SmsController { @Autowired private SmsUtils sms; @PostMapping("/sendSms") public void sendSms(@RequestBody SmsPayload smsPayload) throws Exception { String phoneNumber = smsPayload.getPhoneNumber(); String templateId = smsPayload.getTemplateId(); String[] params = smsPayload.getParams(); sms.send(phoneNumber, templateId, params); } } “` 以上就是 Springboot 整合腾讯云短信服务的示例代码,希望能对你有所帮助!
原创文章,作者:优速盾-小U,如若转载,请注明出处:https://www.cdnb.net/bbs/archives/32212