Python知識分享網(wǎng) - 專業(yè)的Python學(xué)習(xí)網(wǎng)站 學(xué)Python,上Python222
阿里云短信驗(yàn)證碼發(fā)送 PDF 下載
發(fā)布于:2024-02-19 11:19:44
(假如點(diǎn)擊沒反應(yīng),多刷新兩次就OK!)

阿里云短信驗(yàn)證碼發(fā)送  PDF 下載 圖1

 

 

資料內(nèi)容:

 

 

阿里云短信驗(yàn)證碼獲取
1.工具類生成驗(yàn)證碼
 
public class VerificationCodeUtil{
/**
* 六位隨機(jī)數(shù)驗(yàn)證碼
*/
public static String getVerificationCode() {
StringBuilder result = new StringBuilder();
Random random = new Random();
String[] str = {"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9"};
for (int i = 0; i < 6; i++) {
result.append(str[random.nextInt(10)]);
}
return result.toString();
}
}

 

2.短信驗(yàn)證碼發(fā)送工具類
 
public class SmsUtil {
//阿里云服務(wù)KEYID
private static final String KEYID = "******************";
//阿里云服務(wù)SECRET
private static final String SECRET = "*******************";
/**
* 使用AK&SK初始化賬號Client
*/
public static Client createClient(String accessKeyId, String
accessKeySecret) throws Exception {
Config config = new Config()
// 必填,您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 必填,您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 訪問的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new Client(config);
}
/**
* 短信驗(yàn)證碼
*
* @param phone 手機(jī)號
* @param template_code 短信模板
* @param params 模板參數(shù)
*/
public static void sendMessage(String phone, String
template_code, String params) {
try {
// 創(chuàng)建一個(gè)短信服務(wù)客戶端實(shí)例,使用預(yù)設(shè)的KEYID和SECRET進(jìn)行身份認(rèn)
證
Client client = SmsUtil.createClient(KEYID, SECRET);
// 創(chuàng)建SendSmsRequest對象,設(shè)置發(fā)送短信所需參數(shù)
SendSmsRequest sendSmsRequest = new SendSmsRequest()
// 設(shè)置接收短信的手機(jī)號碼
.setPhoneNumbers(phone)
// 設(shè)置模板參數(shù),根據(jù)實(shí)際業(yè)務(wù)場景替換為動態(tài)內(nèi)容
.setTemplateParam(params)
// 設(shè)置短信模板CODE
.setTemplateCode(template_code)
// 設(shè)置短信簽名名稱
.setSignName("短信驗(yàn)證碼");
// 調(diào)用客戶端的sendSmsWithOptions方法發(fā)送短信,并傳入
RuntimeOptions以配置額外的運(yùn)行時(shí)選項(xiàng)
// 這段代碼執(zhí)行后會發(fā)送短信驗(yàn)證碼到指定的手機(jī)號碼上,但此處為了簡潔
沒有打印API返回的結(jié)果
client.sendSmsWithOptions(sendSmsRequest, new
RuntimeOptions());
} catch (Exception ignored) {
}
}
}