SpringBoot 集成阿里云SMS短信发送验证码
pom.xml引入依赖 aliyun-java-sdk
<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>
SmsTest.java
import cn.wxt.wxtong.common.utils.SysCommon;
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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class SmsTest
{
public static void main(String[] args) throws Exception
{
//短信API产品名称,云通信流量服务API产品,开发者无需替换
String product = "Dysmsapi";
//短信API产品域名
String domain = "dysmsapi.aliyuncs.com";
//产品域名,开发者无需替换,此处需要替换成开发者自己的AK (https://ak-console.aliyun.com/)
String accessKeyId = "LTAI3hVb7Gy4V6ga"; //访问秘钥key
String accessKeySecret = "A08rMegYsbLFrUywpVqB0GuKr2NTSa";//访问秘钥secret
// 暂时不支持多Region
String regionId = "cn-hangzhou";
//String bizId = "101"; //流水号
//String action = "SendSms";
String endPointName = "cn-hangzhou";//服务节点
IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
//增加服务节点
DefaultProfile.addEndpoint(endPointName, regionId, product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象
SendSmsRequest request = new SendSmsRequest();
request.setMethod(MethodType.POST);
request.setPhoneNumbers("150xxx");
request.setSignName("短信标题");
request.setTemplateCode("SMS_108240023");//模板Code
//request.setVersion("2014-05-26");
JSONObject json = new JSONObject();
json.put("name","zhangsan");
json.put("code","1000");
request.setTemplateParam(json.toString());
//request.setTemplateParam(json.toJSONString());
request.setOutId("1234");
System.out.println("request" + request);
//
try {
SendSmsResponse response = acsClient.getAcsResponse(request);
if(response.getCode() != null && response.getCode().equals("OK")) {
//请求成功
System.out.println("resp: " + response.toString());
}
}catch (Exception e){
e.printStackTrace();
}
}
}
阿里云短信发送API(SendSms)
https://help.aliyun.com/document_detail/55284.html?spm=a2c4g.11174283.4.4.ynz4a6