springboot 完美整合阿里云短信服务

导入pom文件

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

编写发送短信

@RequestMapping("/test")
@RestController
public class AliyunSendSmsController {
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessSecret}")
    private String accessSecret;
    @Value("${aliyun.SignName}")
    private String SignName;
    @Value("${aliyun.TemplateCode}")
    private String TemplateCode;

    @GetMapping("/{phone}")
    public String sendSms(@PathVariable String phone){
        //1:区域 2:阿里云key 3:阿里云密钥
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);  //设置请求方式
        request.setSysAction("SendSms");    //设置请求的类别
        request.setSysVersion("2017-05-25");    //设置参数版本号
        request.setSysDomain("dysmsapi.aliyuncs.com");  //设置请求域名
        request.putQueryParameter("RegionId", "cn-hangzhou"); //地区
        request.putQueryParameter("PhoneNumbers",phone);    //发送给的手机号
        request.putQueryParameter("SignName",SignName);     //在阿里云申请的签名名称
        request.putQueryParameter("TemplateCode",TemplateCode); //在阿里云申请的模板id
        request.putQueryParameter("TemplateParam","{'code':'123456'}"); //在模板中设置的参数传递, value为json格式
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "";
    }
}
image.png

image.png

image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。