进入阿里云控制台,在云计算基础服务下,找到短信服务
这里有相关参数ACCESSKEY、ACCESSSECRET、SIGNNAME
短信发送可以参考:https://help.aliyun.com/document_detail/55284.html?spm=5176.10629532.106.1.18a51cbeW7Uw5m
try {
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
IClientProfile profile = DefaultProfile.getProfile(CodeUtil.REGIONID, CodeUtil.ACCESSKEY, CodeUtil.ACCESSSECRET);
DefaultProfile.addEndpoint(CodeUtil.REGIONID, CodeUtil.REGIONID, CodeUtil.PRODUCT, CodeUtil.DOMAIN);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象
SendSmsRequest request = new SendSmsRequest();
//使用post提交
request.setMethod(MethodType.POST);
//必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
request.setPhoneNumbers("18810776666");
//必填:短信签名-可在短信控制台中找到
request.setSignName(CodeUtil.SIGNNAME);
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode("code模版");
request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}");
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
//请求成功
}
}catch (ClientException e) {
log.error(e);
}
CodeUtil 工具类
public class CodeUtil {
public static String REGIONID = "cn-hangzhou";//机房信息
public static String ACCESSKEY = "XXXXXXXXXXXXXXXXXXXXXXXX";
public static String ACCESSSECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static String SIGNNAME = "工人屋";//签名名称
public static String PRODUCT = "Dysmsapi";//短信API产品名称
public static String DOMAIN = "dysmsapi.aliyuncs.com";//短信API产品域名
}