一. 阿里云管理平台部分:
第一步:
注册阿里云账号(钉钉或者支付宝或淘宝都可以登录)
第二步:
开通短信服务
第三步:
申请短信签名(说是要两个小时审核,其实半小时到1小时之间就差不多了.~~阿里效率还是很高的)
第四步:
添加短信模板
第五步:
使用云账号登录RAM控制台。
在左侧导航栏的人员管理菜单下,单击用户。
在用户登录名称/显示名称列表下,单击目标RAM用户名称。
在用户AccessKey区域下,单击创建AccessKey。
怎么进入RAM控制台?
创建用户:
添加权限:
后边我们要查看短信接口的api文档,看下怎么对接!
二. 代码部分:
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;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.3</version>
</dependency>
*/
public class SendSms {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
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");
//需要注意的一点TemplateParam不是你在短信模板中的变量,code才是你在短信模板中的设置的变量
JSONObject jsonCode = new JSONObject();
jsonCode.put("code",code);
request.putQueryParameter("TemplateParam", jsonCode.toJSONString());// 验证码
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
完毕 ! 有不明白的地方欢迎下方评论…