短信工具类


public class SmsUtil {

public static String sendSms(String phoneNumber,String code) {
//配置文件类 ,读取配置文件
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", “”, “”);
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"); //查询参数 , 服务器所在地

//设置 手机号
request.putQueryParameter("PhoneNumbers", phoneNumber);
//设置标签名称
request.putQueryParameter("SignName", "爱思华");
//模板 code
request.putQueryParameter("TemplateCode", "SMS_199217541");
//模板是一个json样式
request.putQueryParameter("TemplateParam", "{"code":""+code+""}");

try {
CommonResponse response = client.getCommonResponse(request); //通过客户端拿到 阿里服务器 响应的 短信内容
return response.getData();
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return null;
}

/**
* 获取4位随机数
* @return
*/
public static String getFourRandom(){
Random random = new Random();
String fourRandom = random.nextInt(10000) + "";
System.out.println(fourRandom);
int randLength = fourRandom.length();
if(randLength<4){
for(int i=1; i<=4-randLength; i++)
fourRandom = "0" + fourRandom ;
}
return fourRandom;
}

public static void main(String[] args) {
String fourRandom = getFourRandom();
System.out.print(fourRandom);
}

原文地址:https://www.cnblogs.com/shiyueyangne/p/14240559.html