maven :中的 pom.xml添加java
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.6</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>api
/****** 上面的是JAR包 *******/dom
package com.atguigu.crud.utils;maven
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;ui
public class ALYmessage {
//產品名稱:雲通訊短信API產品,開發者無需替換
static final String product="Dysmsapi";
//產品域名,開發者無需替換
static final String domain = "dysmsapi.aliyuncs.com";
// TODO 此處須要替換成開發者本身的AK(在阿里雲訪問控制檯尋找),下面舉個例子
static final String accessKeyId = "accessKeyId";
static final String accessKeySecret = "accessKeySecret";
//短信控制檯中的簽名和模板的ID
static final String signName ="XXXX";
static final String templateCode ="SMS_XXXXXXXXX";
/**
* 阿里雲短信驗證
* @param phonenumber 手機號碼
* @param code 4~6位的驗證碼
* @return
* @throws ClientException
*/
public static SendSmsResponse sendSms(String phonenumber,String code) throws ClientException{
//可自助調整超時時間
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暫不支持region化
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.setPhoneNumbers(phonenumber);
//必填:短信簽名-可在短信控制檯中找到舉個例子
request.setSignName(signName);
//必填:短信模板id-可在短信控制檯中找到,是id不是名字,舉個例子
request.setTemplateCode(templateCode);
//可選:模板中的變量替換JSON串,如模板內容爲"親愛的${name},您的驗證碼爲${code}"時,此處的值爲--必填,與模板相對應
//request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}");
request.setTemplateParam("{\"code\":\""+code+"\"}");
SendSmsResponse sendSmsResponse=acsClient.getAcsResponse(request);
return sendSmsResponse;
}
}阿里雲
/*************/.net
public Map<String,Object> testShow(@RequestBody Map<String,Object> reqbody) throws ClientException{
Date d = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssS");code
//獲取 100000~999999 中的隨機數
int flag = new Random().nextInt(899999)+100000;orm
//獲取時間格式沒有其餘符號只有數字
String dateNowStr = sdf.format(d);xml
//轉換爲字符串
String code=flag +"";
//調用封裝好的代碼,進行發送短信
SendSmsResponse response = ALYmessage.sendSms(reqbody.get("phonenumber").toString(),code);
//返回成功
if(response.getCode() != null && response.getCode().equals("OK")) { reqbody.put("type", "ok"); return reqbody; } reqbody.put("type", "fail"); return reqbody; }