其他分享
首页 > 其他分享> > 阿里云短信服务发送

阿里云短信服务发送

作者:互联网

pom.xml导入依赖

 <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.3</version>
 </dependency>

Controller文件内测试

@GetMapping("/getCode")
    public void  Code(@RequestParam String number) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<keyid>", "<keysecret>");
        IAcsClient client = new DefaultAcsClient(profile);
        System.out.println(number);
        int code=(int)(1000+Math.random()*9000);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers",number);
        request.putQueryParameter("SignName", "公司名称");
        request.putQueryParameter("TemplateCode", "SMS_204291768");
//        request.putQueryParameter("TemplateParam", "{\"code\":\"code\"}");
        request.putQueryParameter("TemplateParam", "{\"code\":"+code+"}");
        redisTemplate.opsForValue().set(number, String.valueOf(code),50, TimeUnit.MINUTES);


        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

标签:code,短信,request,number,发送,阿里,putQueryParameter,catch,TemplateParam
来源: https://blog.csdn.net/qq_41135753/article/details/110942257