SpringBoot+腾讯云实现短信发送

SpringBoot+腾讯云实现短信发送

  1. 在腾讯云创建短信签名#

    image-20221110093537605

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

    image-20221110093733947

    image-20221110093701327

  3. 短信发送SDK(java)文档,将com.tencentcloudapi包导入#

    Copy
    <dependency> <groupId>com.tencentcloudapi</groupId> <artifactId>tencentcloud-sdk-java</artifactId> <version>3.1.423</version> </dependency>
  4. 在配置文件配置相应的参数信息#

    Copy
    tencent: sms: #secretId keyId: 123 #secretKey keysecret: 123 #SdkAppId smsSdkAppId: 123 #签名内容 signName: "我的技术记录" #模板 ID templateId: 123
  5. 腾讯云短信发送在线Api调试(点击查询对应参数信息)#

    image-20221110094551247

  6. 新建ConstantSmsUtils#

    Copy
    /** * 实现了InitializingBean接口,当spring进行初始化bean时,会执行afterPropertiesSet方法 */ @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; } }
  7. 新建SmsService和SmsServiceImpl#

    Copy
    public interface SmsService { Boolean send (String phone); }
    Copy
    @Service public class SmsServiceImpl implements SmsService { /** * 发送短信 * * @param phone * @return */ @Override public Boolean send(String phone) { //判断手机号是否为空 if (StringUtils.isEmpty(phone)){ return false; } try{ // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密 // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取 Credential cred = new Credential(ConstantSmsUtils.SECRET_ID, ConstantSmsUtils.SECRET_KEY); // 实例化一个http选项,可选的,没有特殊需求可以跳过 HttpProfile httpProfile = new HttpProfile(); httpProfile.setEndpoint("sms.tencentcloudapi.com"); // 实例化一个client选项,可选的,没有特殊需求可以跳过 ClientProfile clientProfile = new ClientProfile(); clientProfile.setHttpProfile(httpProfile); // 实例化要请求产品的client对象,clientProfile是可选的 第二个参数是地域信息 SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile); // 实例化一个请求对象,每个接口都会对应一个request对象 SendSmsRequest req = new SendSmsRequest(); req.setSmsSdkAppid(ConstantSmsUtils.SMSSDKAPP_ID); //设置固定的参数 // 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId req.setSmsSdkAppid(ConstantSmsUtils.SMSSDKAPP_ID); //短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 req.setSign(ConstantSmsUtils.SIGN_NAME); //模板 ID: 必须填写已审核通过的模板 ID req.setTemplateID(ConstantSmsUtils.TEMPLATE_ID); //设置发送相关的参数 String[] phoneNumberSet1 = {"+86"+phone}; //发送的手机号 req.setPhoneNumberSet(phoneNumberSet1); //生成6位数随机验证码 //模板的参数 第一个是验证码,第二个是过期时间,对应短信模板的{1},{2}参数 String[] templateParamSet1 = {"123","1"}; //发送验证码 req.setTemplateParamSet(templateParamSet1); //发送短信 // 返回的resp是一个SendSmsResponse的实例,与请求对象对应 SendSmsResponse resp = client.SendSms(req); System.out.println("resp"+resp); // 输出json格式的字符串回包 System.out.println(SendSmsResponse.toJsonString(resp)); return true; } catch (TencentCloudSDKException e) { e.printStackTrace(); return false; } } }
  8. 编写接口进行测试#

    Copy
    @RestController @RequestMapping("/edusms/sms") public class SmsController { @Autowired private SmsService smsService; @Resource private RedisUtils redisUtils; /** * 短信发送,传入手机号 * * @param phone * @return */ @GetMapping("/send/{phone}") public String sendSms(@PathVariable String phone){ //调用service发送短信的方法 boolean isSend = smsService.send(phone); if (isSend){ return "success"; }else { return "error"; } } }

    image-20221110095644553

image-20221110095723018

posted @   striver-sc  阅读(206)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示
CONTENTS