当前位置:网站首页>阿里云发送短信验证码
阿里云发送短信验证码
2022-07-05 09:02:00 【我要用代码向我喜欢的女孩表白】
3.阿里云发送短信验证码
依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.10</version>
</dependency>
代码(复制即用)
添加自己的id和secret后即可
package com.example.etf.story.service;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teautil.models.*;
import java.util.Random;
public class AliSMS {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的 AccessKey ID
.setAccessKeyId("ss")
// 您的 AccessKey Secret
.setAccessKeySecret("xxx");
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
//生成随机数
/**
*
* @param strSource//加字母和随机数装入集合,原始随机字符串,可自己定义
* @param num //生成几位的验证码,可自己定义
* @return
*/
public static String suijiNum(String strSource,int num){
Random random = new Random();
int maxLength=strSource.length()-1;//随机的索引最大程度,因为索引从0开始,所以最大值的索引 是长度-1
String[] strSources = strSource.split("");//切分字符串为可选的字符数组,用于索引选取
//最终的验证码进行拼接,stringBuffer只创建一个对象节省内存,并且速度快,这个场景不需要线程安全
StringBuffer appendStrEnd = new StringBuffer();
for (int i = 0; i < num; i++) {
int suijiIndex = random.nextInt(maxLength);//生成索引,索引位你的原始字符串中的一个字符的位置
String strAlone = strSources[suijiIndex];//获取字符串中的单个字符值
appendStrEnd.append(strAlone);
}
return appendStrEnd.toString();
}
public static void SendSMSByAli(String phone,String code) throws Exception {
com.aliyun.dysmsapi20170525.Client client = AliSMS.createClient("accessKeyId", "accessKeySecret");
SendSmsRequest sendSmsRequest = new SendSmsRequest()
.setPhoneNumbers(phone)
.setSignName("learning呀朋友")//签名名称【注意别写成模板名称】
.setTemplateCode("SMS_243991108")//模板id
.setTemplateParam("{\"code\":\""+code+"\"}");
RuntimeOptions runtime = new RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
client.sendSmsWithOptions(sendSmsRequest, runtime);
} catch (TeaException error) {
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 如有需要,请打印 error
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
边栏推荐
- Beautiful soup parsing and extracting data
- Programming implementation of subscriber node of ROS learning 3 subscriber
- C# LINQ源码分析之Count
- asp.net(c#)的货币格式化
- Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
- Ros-11 common visualization tools
- Halcon shape_ trans
- Mengxin summary of LCs (longest identical subsequence) topics
- Hello everyone, welcome to my CSDN blog!
- [daiy4] jz32 print binary tree from top to bottom
猜你喜欢
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Halcon shape_ trans
Introduction Guide to stereo vision (5): dual camera calibration [no more collection, I charge ~]
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
It cold knowledge (updating ing~)
What is a firewall? Explanation of basic knowledge of firewall
[code practice] [stereo matching series] Classic ad census: (5) scan line optimization
TF coordinate transformation of common components of ros-9 ROS
[daiy4] copy of JZ35 complex linked list
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
随机推荐
Adaboost使用
C# LINQ源码分析之Count
Huber Loss
皮尔森相关系数
2310. 个位数字为 K 的整数之和
What is a firewall? Explanation of basic knowledge of firewall
Add discount recharge and discount shadow ticket plug-ins to the resource realization applet
迁移学习和域自适应
Programming implementation of ROS learning 5-client node
Editor use of VI and VIM
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
JS asynchronous error handling
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
Jenkins Pipeline 方法(函数)定义及调用
AdaBoost use
Mengxin summary of LIS (longest ascending subsequence) topics
[牛客网刷题 Day4] JZ55 二叉树的深度
Meta tag details
12. Dynamic link library, DLL
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]