首先申请AK
然后导入阿里云短信依赖
implementation 'com.aliyun:dysmsapi20170525:2.0.2'
依赖位置如下
OpenAPI 开发者门户→升级版SDK→Java→安装方式 Gradle Groovy DSL
加入代码块
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.dysmsapi20170525.Client client = Sample.createClient("accessKeyId", "accessKeySecret");
SendSmsRequest sendSmsRequest = new SendSmsRequest();
// 复制代码运行请自行打印 API 的返回值
client.sendSms(sendSmsRequest);
}
注意,要动态请求网络权限,要申请签名和模板,要有AK