一、阿里云权限用户操作
1.开启子用户
2.新建一个用户组(设置添加权限)
3.创建一个用户(具体用来操作的账号)
4.得到Accesskey(id,密码)
二、开通阿里云短信服务
1.找到短信控制面板
2.找到帮助文档
三、添加短信模板
1.短信的具体内容
成功后会有一个模板code
四、添加签名
1.公司名称
五、编写代码
1.建一个springbot工程
2.添加相关依赖
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.dysmsapi.model.v20170525.*;
public class SendSms {
public static void main(String[] args) {
//连接阿里云
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your-access-key-id>", "<your-access-key-secret>");
/** use STS Token
DefaultProfile profile = DefaultProfile.getProfile(
"<your-region-id>", // The region ID
"<your-access-key-id>", // The AccessKey ID of the RAM account
"<your-access-key-secret>", // The AccessKey Secret of the RAM account
"<your-sts-token>"); // STS Token
**/
//构建成一个客户端
IAcsClient client = new DefaultAcsClient(profile);
//构建一个请求
SendSmsRequest request = new SendSmsRequest();
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}