lefengyang
2026-08-09 c71e806d1966a83aad55880c8a456358182a82be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.ruoyi.web.utils;
 
import cn.hutool.core.util.RandomUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.gson.Gson;
 
/**
 * 短信发送工具类
 */
public class SMSUtils {
    public static final String ALIBABA_CLOUD_ACCESS_KEY_ID = "LTAI5tBX45VYcobybXcoKX1F";
    public static final String ALIBABA_CLOUD_ACCESS_KEY_SECRET = "jS4UwR2pB3ucKzlnDO6y0uwv2qHggT";
 
    /**
     * 使用AK&SK初始化Client
     * 使用客户端去发送短信
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 必填,您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填,您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名 固定写法
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }
 
    /**
     * 向指定手机号发送短信
     *
     * @param signName     签名
     * @param templateCode 模板
     * @param phoneNumbers 手机号
     * @param param        参数 ---验证码
     */
    public static void sendMessage(String signName, String templateCode, String phoneNumbers, String param) throws Exception {
        // 获取客户端对象
        Client client = createClient(ALIBABA_CLOUD_ACCESS_KEY_ID, ALIBABA_CLOUD_ACCESS_KEY_SECRET);
        // 获取发送短信对象 设置发送短信的 签名,模板,手机号,验证码
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(signName);
        request.setTemplateCode(templateCode);
        request.setPhoneNumbers(phoneNumbers);
        request.setTemplateParam("{\"code\":\"" + param + "\"}");
        // 运行时行为  例如最大尝试次数,超时时间等等
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 根据信息发送短信
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(request, runtime);
            System.out.println(new Gson().toJson(sendSmsResponse.body));
        } catch (TeaException error) {
            // 如有需要,请打印 error
            System.out.println(error);
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要,请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
    }
 
 
    /**
     * 随机数
     */
    public static String randomNum() {
        // 获得四位的随机整数
        return RandomUtil.randomNumbers(4);
    }
 
    public static void main(String[] args) throws Exception {
        String s = randomNum();
        System.out.println(s);
        sendMessage("深圳本我","SMS_274905251","18258070687",s);
    }
}