当前位置: 首页 > article >正文

springboot如何接入阿里云短信

登录阿里云开通短信模块
在这里插入图片描述
得到审核通过的签名
在这里插入图片描述
审核通过后,阿里云会赠送一条验证码模板,可以直接使用,如果有其他业务情况,可自行申请
在这里插入图片描述
引入阿里云短信依赖
pom.xml

<!--引入阿里云短信-->
<dependency>
	<groupId>com.aliyun</groupId>
	<artifactId>dysmsapi20170525</artifactId>
	<version>2.0.18</version>
</dependency>

短信类代码如下:

public class SendMsg {
    private static final String accessKeyId = "你的KeyId";
    private static final String accessKeySecret = "你的KeySecret";

    /**
     * 使用AK&SK初始化账号Client
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {

        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
	/**
	* 发送短信验证码
	**/
    public static boolean send(String mobies, String content) {
        try {
            com.aliyun.dysmsapi20170525.Client client = SendMsg.createClient();
            com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest();
            sendSmsRequest.setSignName("你的短信去签名,例如【中国移动】")
                    .setTemplateCode("你的短信模板code")
                    .setPhoneNumbers(mobies)
                    .setTemplateParam("{\"code\":\"" + content + "\"}");
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response = client.sendSmsWithOptions(sendSmsRequest, runtime);
            if ("OK".equals(response.getBody().getCode())) {
                return true;
            } else {
                System.out.println(response.getBody().getCode());
                return false;
            }
        } catch (TeaException error) {
            // 如有需要,请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            error.printStackTrace();
            return false;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要,请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            error.printStackTrace();
            return false;
        }
    }
}

最终效果:
在这里插入图片描述


http://www.kler.cn/news/354686.html

相关文章:

  • Vue 3 中的状态管理:深入探讨 Vuex 和 Pinia 的比较与最佳实践
  • 初识git · 有关模型
  • 【C语言】数据类型
  • 实用篇:如何让Win11右键默认显示更多呢
  • STM32 独立看门狗和窗口看门狗区别
  • Python进阶知识
  • 智能平台或系统中的归因、根因分析案例集锦
  • 使用python实现图书管理系统
  • Unity动画系统
  • 外包干了3周,技术退步太明显了。。。。。
  • 使用React Router实现前端的权限访问控制
  • 【Flutter】Dart:异步
  • docker容器里的时间不对,linux解决方案
  • 机器学习——向量化
  • 学习第三十六行
  • 【实战案例】树形字典结构数据的后端解决方案
  • 雷达数据与影像数据直观对比
  • YOLO的更新迭代
  • 基于SpringBoot的企业客户管理系统的设计与实现(论文+源码)_kaic
  • 软件测试CNAS实验室软件维护性测试作业指导书怎么写