如果使用maven管理,引入aliyun-java-sdk-core-4.5.7.jar
如果不使用maven管理,引入aliyun-java-sdk-core-4.5.7.jar
opentracing-api-0.33.0.jar
opentracing-noop-0.33.0.jar
opentracing-util-0.33.0.jar
package com.frameboot.util.sms;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import java.io.IOException;
import java.util.Map;
/**
* @author ywd
* @create 2020-11-10 13:31
*/
public class SendSms {
//1.模板
public static String verificationCode="SMS_111111";
public static String payMessage="SMS_11111111";
public static String confirmMessage="SMS_1111111";
public static String send(String mobile, String conent,String templateCode) throws IOException {
//2.传入自己id和秘钥
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "id", "secret");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", mobile);
//3.修改签名
request.putQueryParameter("SignName", "签名");
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", "{\"code\":\""+conent+"\"}");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
Map map= JSON.parseObject(response.getData(), Map.class);
if( map.get("Code").equals("OK")){
return "200";
}else{
return map.get("Code").toString();
}
} catch (ServerException e) {
e.printStackTrace();
return "201";
} catch (ClientException e) {
e.printStackTrace();
return "201";
}
}
//4.验证码不要直接传入111111,222222之类的信息,会被阿里云拦截
public static void main(String[] args) {
try {
send("手机号","验证码",verificationCode);
} catch (IOException e) {
e.printStackTrace();
}
}
}