2023年阿里云短信服務(wù)個人申請模板已經(jīng)申請不到了,現(xiàn)在使用測試模板實現(xiàn)發(fā)送短信功能
但你手機上接收到的是模板字符串,具體驗證碼可以自行存到Redis或者輸出到控制臺
步驟:
平臺鏈接:
https://dysms.console.aliyun.com/quickstart
依次點擊下面圖片的提示
設(shè)置測試模板參數(shù)
?
?
代碼實現(xiàn):
?controller層
//發(fā)送短信的方法 @GetMapping("send/{phone}") public R sendSms(@PathVariable String phone){ //1、從redis獲取驗證碼,如果獲取到直接返回 String code = redisTemplate.opsForValue().get(phone); if (!StringUtils.isEmpty(code)) { return R.ok().message("短信已發(fā)送"); } //2 如果redis獲取 不到,進行阿里云發(fā)送 //生成隨機值,傳遞阿里云進行發(fā)送 code = RandomUtil.getFourBitRandom(); Map<String,Object> param = new HashMap<>(); param.put("code", code); boolean isSend = smsService.send(phone, "SMS_154950909", param); if(isSend) { //保存到Redis redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES); return R.ok().message("發(fā)送短信成功"); } else { return R.error().message("發(fā)送短信失敗"); } } }
?service接口
?
boolean send(String phone, String sms_154950909, Map<String, Object> param);
?service實現(xiàn)文章來源:http://www.zghlxwxcb.cn/news/detail-621254.html
@Override public boolean send(String phone, String templateCode, Map<String, Object> param) { DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your-access-key-id>", "<your-access-key-secret>"); IAcsClient client = new DefaultAcsClient(profile); SendSmsRequest request = new SendSmsRequest(); request.setSignName("阿里云短信測試"); request.setTemplateCode("SMS_154950909"); request.setPhoneNumbers("***********"); request.setTemplateParam("{\"code\":\"1234\"}"); try { SendSmsResponse response = client.getAcsResponse(request); System.out.println(new Gson().toJson(response)); //獲得響應(yīng)狀態(tài)碼,返回true return response.getCode().equals("OK"); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); } return false; }
這樣你的手機就能接收到短信了~~~~~~~文章來源地址http://www.zghlxwxcb.cn/news/detail-621254.html
到了這里,關(guān)于阿里云短信服務(wù)---測試模板實現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!