阿里云发送短信

需要申请的东西:签名 模版

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.bohu.utils;

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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.netflix.client.ClientException;
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* @ClassName SMSUtils
* @Author shenbohu
* @Date 2021/6/139:57 下午
* @Version 1.0
**/
@Component
@Data
public class SMSUtils implements InitializingBean {

@Value("${aliyun.keysecret}")
private String keysecret;

@Value("${aliyun.keyid}")
private String keyId;


public static String KEYID;
public static String KEYSECRET;
// public static final String VALIDATE_CODE = "SMS_175061136";//发送短信验证码
// public static final String ORDER_NOTICE = "SMS_175051399";
//........

/**
* 发送短信
*
* @param phoneNumbers
* @param param
* @throws ClientException
*/
public static boolean sendShortMessage(String templateCode, String phoneNumbers, String param ,String SignName) {
// 设置超时时间-可自行调整
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
final String product = "Dysmsapi";
final String domain = "dysmsapi.aliyuncs.com";

final String accessKeyId = KEYID;// 你的accessKeyId
final String accessKeySecret = KEYSECRET;// 你的accessKeySecret
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
try {
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
} catch (com.aliyuncs.exceptions.ClientException e) {
e.printStackTrace();
}
IAcsClient acsClient = new DefaultAcsClient(profile);
// 组装请求对象
SendSmsRequest request = new SendSmsRequest();
// 使用post提交
request.setMethod(MethodType.POST);
request.setPhoneNumbers(phoneNumbers);
// 必填:短信签名-可在短信控制台中找到
request.setSignName(SignName);
// 必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templateCode);
request.setTemplateParam("{\"code\":\"" + param + "\"}");
SendSmsResponse sendSmsResponse = null;
try {
sendSmsResponse = acsClient.getAcsResponse(request);
} catch (com.aliyuncs.exceptions.ClientException e) {
e.printStackTrace();
}
if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
// 请求成功
System.out.println("请求成功");
return true;
}
return false;
}

@Override
public void afterPropertiesSet() throws Exception {
KEYID = keyId;
KEYSECRET = keysecret;

}
}

​```java
//测试类
package com;


import org.aspectj.weaver.ast.Var;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.DigestUtils;

public class text {
public static void main(String[] args)throws Exception {
SMSUtils.sendShortMessage("模版CODE","发送的手机号","发送的消息");


}
}

上次更新 2022-02-22