这里假设已经申请好短信服务和模板。
1.添加maven依赖
<!-- aliyun短信依赖 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.24</version>
</dependency>
2.添加短信客户端配置类
package com.example.demo.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author programmer
* @version 1.0
* @create 2024/5/15 17:27
* @description aliyun短信配置类
* @since 1.0
**/
@Configuration
public class AliYunSmsConfig {
@Value("${aliyun.sms.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.sms.accessKeySecret}")
private String accessKeySecret;
/**
* 短信客户端Client
*/
@Bean
public com.aliyun.dysmsapi20170525.Client aliyunSmsClient() throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
//访问的域名, Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
}
3.实现一个短信工具类
package com.example.demo.utils;
import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author programmer
* @version 1.0
* @create 2024/5/15 18:25
* @description aliyun短信发送类
* @since 1.0
**/
@Slf4j
@Component
public class AliyunSmsUtil {
@Autowired
private Client smsClient;
@Value("${aliyun.sms.signName}")
private String signName; //短信签名名称
@Value("${aliyun.sms.templateCode.business}")
private String businessTemplateCode; //短信模板Code
/**
* 发送短信
* @param phoneNumbers 支持对多个手机号码发送短信,手机号码之间以半角逗号(,)分隔。
* (如果 JSON 中需要带换行符,请参照标准的 JSON 协议处理)
* @param signName 短信签名名称
* @param templateCode 短信模板code
* @param TemplateParam 短信模板参数,格式:json字符串.
* 示例: {"name":"张三","number":"1390000****"}
*/
public void sendSms(String phoneNumbers, String signName, String templateCode, String TemplateParam){
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setPhoneNumbers(phoneNumbers) //支持对多个手机号码发送短信,手机号码之间以半角逗号(,)分隔
.setSignName(signName) //短信签名名称
.setTemplateCode(templateCode)//短信模板Code
.setTemplateParam(TemplateParam); //短信模板变量对应的实际值。支持传入多个参数。
try {
SendSmsResponse sendSmsResponse = smsClient
.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
log.info("推送短信响应结果: "+new Gson().toJson(sendSmsResponse.getBody()));
/** sendSmsResponse body格式:
* {
* "Code": "OK", //请求状态码,返回 OK 代表请求成功
* "Message": "OK", //状态码的描述
* "BizId": "9006197469364984****", //发送回执 ID
* "RequestId": "F655A8D5-B967-440B-8683-DAD6FF8DE990" //请求 ID
* }
*/
} catch (TeaException error) {
log.error(error.getMessage());
log.error("诊断地址:{}", error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
log.error(error.getMessage());
log.error("诊断地址:{}", error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
/**
* 发送短信
* @param phoneNumbers 支持对多个手机号码发送短信,手机号码之间以半角逗号(,)分隔。
* (如果 JSON 中需要带换行符,请参照标准的 JSON 协议处理)
* @param signName 短信签名名称
* @param templateCode 短信模板code
* @param object 短信模板参数实体类
*/
public void sendSms(String phoneNumbers,String signName, String templateCode,Object object){
if (object instanceof CharSequence){
sendSms(phoneNumbers,signName, templateCode, object);
}else {
sendSms(phoneNumbers,signName, templateCode, JSONUtil.parse(object).toStringPretty());
}
}
/**
* 发送业务短信
* @param phoneNumbers 手机号码,持对多个手机号码发送短信半角逗号(,)分隔
* @param paramObj 短信模板参数
*/
public void sendBusinessSms(String phoneNumbers, Object paramObj){
sendSms(phoneNumbers, signName, businessTemplateCode, paramObj);
}
}
4.在配置文件中添加配置,这里是application.properties文件
aliyun.sms.accessKeyId=你的阿里云accessKeyId
aliyun.sms.accessKeySecret=你的阿里云accessKeySecret
aliyun.sms.signName=申请的短信签名
aliyun.sms.templateCode.business=123456 #业务短信模板Code