当前位置:网站首页>阿里云发送短信验证码
阿里云发送短信验证码
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);
}
}
}
边栏推荐
- Attention is all you need
- [Niuke brush questions day4] jz55 depth of binary tree
- Ecmascript6 introduction and environment construction
- Array, date, string object method
- 编辑器-vi、vim的使用
- Halcon wood texture recognition
- Golang foundation -- map, array and slice store different types of data
- Confusing basic concepts member variables local variables global variables
- C#图像差异对比:图像相减(指针法、高速)
- ECMAScript6介绍及环境搭建
猜你喜欢
Halcon Chinese character recognition
Summary and Reflection on issues related to seq2seq, attention and transformer in hands-on deep learning
RT thread kernel quick start, kernel implementation and application development learning with notes
Count of C # LINQ source code analysis
混淆矩阵(Confusion Matrix)
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
Redis实现高性能的全文搜索引擎---RediSearch
[beauty of algebra] singular value decomposition (SVD) and its application to linear least squares solution ax=b
Huber Loss
Rebuild my 3D world [open source] [serialization-3] [comparison between colmap and openmvg]
随机推荐
Halcon snap, get the area and position of coins
Meta tag details
2011-11-21 training record personal training (III)
Codeworks round 681 (Div. 2) supplement
Applet (subcontracting)
[Niuke brush questions day4] jz55 depth of binary tree
Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
Task failed task_ 1641530057069_ 0002_ m_ 000000
ROS learning 1- create workspaces and function packs
RT thread kernel quick start, kernel implementation and application development learning with notes
Editor use of VI and VIM
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Adaboost使用
驾驶证体检医院(114---2 挂对应的医院司机体检)
C#图像差异对比:图像相减(指针法、高速)
ECMAScript6介绍及环境搭建
Codeforces round 684 (Div. 2) e - green shopping (line segment tree)
TF coordinate transformation of common components of ros-9 ROS
np.allclose
Applet (use of NPM package)