1、第一步毋庸置疑的在阿里云控制台,短信服务,申请签名、短信模板,然后创建AccessKey 点击官网
package com.alisms.test.sms;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.alibaba.fastjson.JSONObject;
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;
/**
* @title 阿里云短信测试
* @author yaco_fyk@163.com
* @date 2019年6月17日 上午9:41:39
*/
/**
<!-- 阿里云短信sdk -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
*/
public class TestAliSms {
private static final String accessKey = "LTAIu**********RZ";
private static final String secret = "FNw0**********t2G**********";
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("default", accessKey,
secret);
// 初始化client对象
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
// 产品域名,开发者无需替换
request.setDomain("dysmsapi.aliyuncs.com");
// 系统参数 不可以自己改动时间 new SimpleDateFormat("yyyy-MM-dd").format(new Date()) 必须是2017-5-25
request.setVersion("2017-05-25");
// 系统规定参数
request.setAction("SendSms");
// 发送的电话号码
request.putQueryParameter("PhoneNumbers", "130*******6");
// 签名管理->签名名称
request.putQueryParameter("SignName", "XXX");
// 模版管理->模版CODE
request.putQueryParameter("TemplateCode", "SMS_16*****255");
JSONObject jsonObject = new JSONObject();
jsonObject.put("code", "8888");
request.putQueryParameter("TemplateParam", jsonObject.toJSONString());
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
JSONObject json = JSONObject.parseObject(response.getData());
if ("OK".equals(json.get("Code"))) {
System.out.println("发送成功...");
} else {
System.out.println("发送失败,失败消息为" + json.get("Message"));
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}