<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.6</version> <!-- 注:如提示报错,先升级基础包版,无法解决可联系技术支持 -->
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
/**
* Author: LiuLin
* Date: 2022/4/13 15:05
* Description:
*/
public enum DySmsEnum {
/**
* 发送短信使用的模板,使用的签名
*/
LOGIN_TEMPLATE_CODE("SMS_****", "**化学");
/**
* 短信模板编码
*/
String templateCode;
/**
* 签名
*/
String signName;
DySmsEnum(String templateCode, String signName) {
this.templateCode = templateCode;
this.signName = signName;
}
public String getTemplateCode() {
return templateCode;
}
public String getSignName() {
return signName;
}
}
import com.alibaba.fastjson.JSONObject;
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.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Author: LiuLin
* Date: 2022/4/13 15:06
* Description:
*/
@Component
public class DySmsHelper {
private final static Logger logger = LoggerFactory.getLogger(DySmsHelper.class);
private static final String product = "Dysmsapi";//产品名称:云通信短信API产品,无需替换
private static final String domain = "dysmsapi.aliyuncs.com";//产品域名,无需替换
private static final String accessKeyId = "你的accessKeyId";
private static final String accessKeySecret = "你的accessKeySecret";
public boolean sendSms(String phone, JSONObject templateParamJson, DySmsEnum dySmsEnum) throws ClientException {
//可自助调整超时时间
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);
//验证json参数
//validateParam(templateParamJson, dySmsEnum);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(phone);
//必填:短信签名-可在短信控制台中找到
request.setSignName(dySmsEnum.getSignName());
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(dySmsEnum.getTemplateCode());
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(templateParamJson.toJSONString());
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCode("90997");
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
//request.setOutId("yourOutId");
boolean result = false;
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
logger.info("短信接口返回的数据----------------");
logger.info("{Code:" + sendSmsResponse.getCode() + ",Message:" + sendSmsResponse.getMessage() + ",RequestId:" + sendSmsResponse.getRequestId() + ",BizId:" + sendSmsResponse.getBizId() + "}");
if ("OK".equals(sendSmsResponse.getCode())) {
result = true;
}
return result;
}
}
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Author: LiuLin
* Date: 2022/4/13 14:35
* Description:
*/
@RestController
@RequestMapping("/public")
public class Test {
private final DySmsHelper dySmsHelper;
public Test(DySmsHelper dySmsHelper) {
this.dySmsHelper = dySmsHelper;
}
@GetMapping("/sendSms")
public void sendSms() throws ClientException {
JSONObject obj = new JSONObject();
obj.put("code", "123456");
dySmsHelper.sendSms("", obj, DySmsEnum.LOGIN_TEMPLATE_CODE);
}
}