1.登入阿裡雲 https://www.aliyun.com/
2.搜尋短信服務 打開控制台

3.添加簽名
4.添加模闆 !!!注意添加成功後會有 1-2小時内稽核
5.稽核完畢後
6.點選頭像 添加AK
7.引入jar包依賴
<!-- 短信驗證碼 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.3.1</version>
</dependency>
<!-- 短信驗證碼 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.0.0</version>
</dependency>
8.編寫工具類 執行main方法 注意填寫對應的參數
package com.czxy.util;
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;
/**
* @Author fan
* @Date 2020/7/4 20:34:29
* @Version 1.0
*/
public class AliYunSmsUtils {
public AliYunSmsUtils() {
super();
// TODO Auto-generated constructor stub
}
//産品名稱:雲通信短信API産品,開發者無需替換
static final String product = "Dysmsapi";
//産品域名,開發者無需替換
static final String domain = "dysmsapi.aliyuncs.com";
// TODO 此處需要替換成開發者自己的AK(在阿裡雲通路控制台尋找)
static final String accessKeyId = "添加的AKid"; // TODO 修改成自己的accessKeyId
static final String accessKeySecret = "添加的AKSecret"; // TODO 修改成自己的accessKeySecret
public static SendSmsResponse sendSms(String telephone, 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(telephone);
//必填:短信簽名-可在短信控制台中找到
request.setSignName("自己的signName"); // TODO 修改成自己的簽名
//必填:短信模闆-可在短信控制台中找到
request.setTemplateCode("自己的templateCode"); // TODO 修改成自己的模闆CODE
//可選:模闆中的變量替換JSON串,如模闆内容為"親愛的${name},您的驗證碼為${code}"時,此處的值為
// request.setTemplateParam("{\"name\":\"Tom\", \"code\":\"123\"}");
request.setTemplateParam("{\"code\":\"" + code + "\"}");
//選填-上行短信擴充碼(無特殊需求使用者請忽略此字段)
//request.setSmsUpExtendCode("90997");
//可選:outId為提供給業務方擴充字段,最終在短信回執消息中将此值帶回給調用者
request.setOutId("yourOutId");
//hint 此處可能會抛出異常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
System.out.println("短信發送成功!");
}else {
System.out.println("短信發送失敗!");
}
return sendSmsResponse;
}
//以下為測試代碼,随機生成驗證碼
private static int newcode;
public static int getNewcode() {
return newcode;
}
public static void setNewcode(){
newcode = (int)(Math.random()*1000000); //每次調用生成一位六位數的随機數
}
public static void main(String[] args) throws ClientException, InterruptedException {
setNewcode();
String code = Integer.toString(getNewcode());
System.out.println("發送的驗證碼為:"+code);
//發短信
SendSmsResponse response =sendSms("要發送的電話号碼",code); // TODO 填寫你需要測試的手機号碼
System.out.println("短信接口傳回的資料----------------");
System.out.println("Code=" + response.getCode());
System.out.println("Message=" + response.getMessage());
System.out.println("RequestId=" + response.getRequestId());
System.out.println("BizId=" + response.getBizId());
}
}
9.效果