引入依赖
<!--ailiyunSMS-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-openapi</artifactId>
<version>0.0.10</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-console</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-util</artifactId>
<version>0.2.10</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea</artifactId>
<version>[1.0.3, 2.0.0)</version>
</dependency>
SmsUtils
发送短信工具类
package com.hxecm.util;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.tea.TeaModel;
import com.aliyun.teaopenapi.models.*;
import com.hxecm.dto.AlarmMsgDto;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Auther: Allen
* @Date: 2021/2/23 13:42
* @Description: 短信发送
*/
public class SmsUtils {
/**
* 阿里云服务参数
*/
private static final String ACCESS_KEY_ID = "ACCESS_KEY_ID";
private static final String ACCESS_KEY_SECRET = "ACCESS_KEY_SECRET";
/** 通用签名 */
private static final String Sign_Name = "Sign_Name";
/** 验证码模板 */
private static final String Ver_Template_Code = "Ver_Template_Code";
/** 短信通知模板 */
private static final String Notice_Template_Code = "Notice_Template_Code";
/**
* common - 使用AK&SK初始化账号Client
*
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.dysmsapi20170525.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 com.aliyun.dysmsapi20170525.Client(config);
}
// ######################################################
// 发送手机验证码验证
// ######################################################
/**
* 随机生成验证码
*/
private static int newCode;
public static int getNewCode() {
return newCode;
}
/**
* 每次调用生成一次四位数的随机数验证码
*/
public static void setNewCode() {
newCode = (int) ((Math.random() * 9 + 1) * 1000);
}
/**
* 发送短信验证码
*
* @return
* @throws Exception
*/
public static SendSmsResponse sendSMS(String tel,String smsCode) throws Exception {
com.aliyun.dysmsapi20170525.Client client = SmsUtils.createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(tel)
.setSignName(Sign_Name)
.setTemplateCode(Ver_Template_Code)
.setTemplateParam("{\"code\":\"" + smsCode + "\"}");
SendSmsResponse resp = client.sendSms(sendSmsRequest);
return resp;
}
/**
* 获取随机验证码
* @return
*/
public static String getSmsCode(){
//随机验证码
setNewCode();
String smsCode = Integer.toString(getNewCode());
return smsCode;
}
// ######################################################
// 发送手机短信通知
// ######################################################
public static SendSmsResponse sendSMSNotification(String phone, AlarmMsgDto alarmMsgDto) throws Exception {
com.aliyun.dysmsapi20170525.Client client = SmsUtils.createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(phone)//phone可为多个,中间用","连接
.setSignName(Sign_Name)
.setTemplateCode(Notice_Template_Code)
.setTemplateParam("{\"context\":\"context\"}");
SendSmsResponse resp = client.sendSms(sendSmsRequest);
return resp;
}
}
SendSmsResponse ,是否发送成功检验
if (resp.getBody().getCode() != null && resp.getBody().getCode().equals("OK")) {
log.info("SUCCESS");
} else {
log.info("ERROR");
}