需要开通阿里云 短信服务的模板和签名
//需要导入的jar
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
//自己封装的code类
package com.how2j.copy.vo;
public class Code {
private String code;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
//官方测试使用的代码 稍作修改 即可
package com.how2j.copy.util;
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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.how2j.copy.vo.Code;
public class AliyunSmsUtils {
//这些都改为自己对应的
private static String mySign = "***";
private static String myTemplate = "*";
private static String accessKey = "*****N";
private static String accessSecret = "S*******g";
public static boolean send(String code,String target){
Code code1 = new Code();
code1.setCode(code);
String body = JSONObject.toJSONString(code1);
// System.out.println(body);
// System.out.println("{\"code\":\"4124\"}");
/***************************/
//区域 阿里云测试上并不是必须要填写的
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKey, accessSecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
//发送的目标
request.putQueryParameter("PhoneNumbers", target);
//签名名字,需要改成自己的
request.putQueryParameter("SignName", mySign);
//模板名字,需要改成自己的
request.putQueryParameter("TemplateCode", myTemplate);
//发送的验证码的code 需要遵守JSON格式
request.putQueryParameter("TemplateParam", body);
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (com.aliyuncs.exceptions.ServerException e) {
e.printStackTrace();
return false;
} catch (ClientException e) {
e.printStackTrace();
return false;
}
return true;
/****************************/
}
public static void main(String[] args) {
send("133455",139**66***01L+"");
/*
//区域 阿里云测试上并不是必须要填写的
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
"LTAIH4zVEBDjvxXN", "SkjcM446fhobxCUWNvoVkeuXquOryg");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
//发送的目标
request.putQueryParameter("PhoneNumbers", "139*******01");
//签名名字,需要改成自己的
request.putQueryParameter("SignName", "1*******");
//模板名字,需要改成自己的
request.putQueryParameter("TemplateCode", "SMS_16*****85");
//发送的验证码的code 需要遵守JSON格式
request.putQueryParameter("TemplateParam", "{\"code\":\"4124\"}");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (com.aliyuncs.exceptions.ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}*/
}
}