springboot2入门到实战-短信发送

springboot整合短信

申请阿里云短信服务

短信服务 (aliyun.com)

image-20230622204159899

image-20230622204421491

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

image-20230622204537281

image-20230622204613437 image-20230622204613437

测试短信服务

image-20230622205024098

image-20230622205100611

image-20230622205202206

image-20230622205239652

image-20230622205316934

image-20230622205422136

image-20230622205556613

image-20230622205809534

image-20230622205830161 image-20230622205854321

image-20230622210023642

获取阿里云的accessKey

访问地址:RAM 访问控制 (aliyun.com)

image-20230622213507041

短信模板

image-20230622214024992

springboot整合阿里云短信

引入依赖

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

代码实现

 DefaultProfile profile = DefaultProfile.getProfile(
                "cn-qingdao",
                "", //AccessIdKey
                ""); //AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        //下面这3个不要改动
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        //接收短信的手机号码
        request.putQueryParameter("PhoneNumbers","电话号码");//此处替换为电话号码
        //短信签名名称
        request.putQueryParameter("SignName","阿里云短信测试");
        //短信模板ID
        request.putQueryParameter("TemplateCode","SMS_154950909");
        //短信模板变量对应的实际值 ${code} 中的值
        Map<String,String> param = new HashMap<>(2);
        param.put("code", String.valueOf(new Random().nextInt(80000)+10000)); //写入的短信内容
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

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

image-20230625140954129

测试结果

image-20230622220327151

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值