标签:console aliyun str param void stat def pre end
今天无聊自己看教程写了java发送验证码的代码!
导入依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
代码 我写注释的地方进行你自己的替换,其他地方不动,写main方法调用sendShortMessage()此方法!
templateCode是模板code,phoneNumbers是手机号,name是你传入的参数;
网址申请签名模板的地方:https://dysms.console.aliyun.com/dysms.htm?spm=5176.8195934.1283918..11ae30c9C89sbR&aly_as=B-ULrcFFP#/domestic/text/sign
没有企业资质只能申请验证码类型的签名,模板申请验证码的模板,就算申请短信通知模板也用不了。
public static void sendShortMessage(String templateCode,String phoneNumbers,String name) throws ClientException{
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
final String product = "Dysmsapi";
final String domain = "dysmsapi.aliyuncs.com";
final String accessKeyId = "accssKeyId"; // 你的accessKeyId 把我的替换掉
final String accessKeySecret = "accessKeySecret";// 你的accessKeySecret 把我的替换掉
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setMethod(MethodType.POST);
request.setPhoneNumbers(phoneNumbers); //这是所发短信的手机号
request.setSignName("签名"); //这是签名名称,换成你自己的
request.setTemplateCode(templateCode); //这是模板code,同样换成自己的,
request.setTemplateParam("{\"code\":\""+name+"\"}"); //code是短信的里面你要替换的参数名称,name则是你真正的参数; json格式
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
System.out.println(sendSmsResponse.getCode());
if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
// 请求成功
System.out.println("请求成功");
}
}
标签:console aliyun str param void stat def pre end
原文地址:https://www.cnblogs.com/x-x-f/p/12692186.html