基于SpringBoot调用阿里云短信接口实现发送短信功能

2023年5月24日09:05:43

1、准备工作

1.1、流程
基于SpringBoot调用阿里云短信接口实现发送短信功能
1.2、开通短信服务
基于SpringBoot调用阿里云短信接口实现发送短信功能
1.3、申请认证密钥
基于SpringBoot调用阿里云短信接口实现发送短信功能
1.4、
申请短信签名
基于SpringBoot调用阿里云短信接口实现发送短信功能
1.5、申请短信模板
基于SpringBoot调用阿里云短信接口实现发送短信功能

2、Controller层接口

    @GetMapping("sendSMS")
    public void getUser() {
        //生成验证码 1-9 6
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            builder.append(new Random().nextInt(9) + 1);
        }
        String smsCode = builder.toString();

        try {
            //发送短信 {"code":"123456"}
            SmsUtil.sendSms("18319143841", "自己在阿里云申请的签名名称", "填写自己申请的模板code", smsCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3、SmsUtils(发送短信工具类)

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SmsUtil {
    //替换成自己申请的accessKeyId
    private static String accessKeyId = "LTAI4GHw8BMP1RhZwYKedBRu";
    //替换成自己申请的accessKeySecret
    private static String accessKeySecret = "1F5zjL1Ke4DIHVklZmOygnx5z2yKpA";

    static final String product = "Dysmsapi";
    static final String domain = "dysmsapi.aliyuncs.com";

    /**
     * 发送短信
     *
     * @param phoneNumbers 要发送短信到哪个手机号
     * @param signName     短信签名[必须使用前面申请的]
     * @param templateCode 短信短信模板ID[必须使用前面申请的]
     * @param param        模板中${code}位置传递的内容
     */
    public static void sendSms(String phoneNumbers, String signName, String templateCode, String param) {
        try {
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");

            //初始化acsClient,暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(phoneNumbers);
            request.setSignName(signName);
            request.setTemplateCode(templateCode);
            request.setTemplateParam(param);
            request.setOutId("yourOutId");
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
            if (!"OK".equals(sendSmsResponse.getCode())) {
                log.info("发送短信失败,{}", sendSmsResponse);
                throw new RuntimeException(sendSmsResponse.getMessage());
            }
        } catch (Exception e) {
            log.info("发送短信失败,{}", e);
            throw new RuntimeException("发送短信失败");
        }
    }
}

4、pom.xml新增短信坐标

  <!--短信发送-->
     <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-alicloud-sms</artifactId>
     </dependency>

  • 作者:yzq-3841
  • 原文链接:https://blog.csdn.net/weixin_43272286/article/details/108397844
    更新时间:2023年5月24日09:05:43 ,共 2302 字。