springboot+阿里云发送短信
小开开 2020-08-07 13:10:32 2020-08-07 210 0
1.准备工作,需要购买短信以及新增短信模板
2.maven导入springboot sdk <!--阿里云短信--> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.0.3</version> </dependency>
3.java代码
@GetMapping("/sentMsgCode") public RestResult sentMsgCode(@RequestParam(name = "phone") String phone) { String vailCode = "123456"; JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("code",vailCode); //开通短信时的accessKeyId,secret DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "accessKeyId", "secret"); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("RegionId", "cn-hangzhou"); request.putQueryParameter("PhoneNumbers", phone); request.putQueryParameter("SignName", "代码网"); //这里填写审核通过后短信模板的code request.putQueryParameter("TemplateCode", "SMS_189033695"); request.putQueryParameter("TemplateParam", jsonObject.toString()); try { CommonResponse response = client.getCommonResponse(request); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } return RestResult.success(); }