package com.diplomaproject.sms.utills;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class SmsUtill {
//产品名称:云通信短信API产品,开发者无需替换
static final String product="Dysmsapi";
//产品域名,开发者无需替换
static final String domain="dysmsapi.aliyuncs.com";
//区域id,短信API的值为:cn-hangzhou
static final String RegionId="cn-hangzhou";
static final String AccessKeyID="LTAI4Fu9FyW333jG1g3vryVW";
static final String AccessKeySecret="tZQM6rLclYvzv7MObUy8GnBvnEON9X";
/* //此注解可从yml中取值
@Autowired
private Environment env;*/
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.3.3</version>
</dependency>
*/
/*
* 手机号
* 签名名称
*模版CODE
*验证码param
* */
public void SendSms(String mobile, String signName, String templateCode, String param) {
//取出yml中配置的AccessKeyID,AccessKeySecret
// String AccessKeyID = env.getProperty("aliyun.sms.AccessKeyID");
// String AccessKeySecret = env.getProperty("aliyun.sms.AccessKeySecret");
DefaultProfile profile = DefaultProfile.getProfile(RegionId, AccessKeyID, AccessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain(domain);
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", mobile);
request.putQueryParameter("SignName", signName);
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", param);
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SmsUtill smsUtill = new SmsUtill();
smsUtill.SendSms("17736061816","***","*****","{\"checkcode\":\""+123456+"\"}");
}
}