当前位置:网站首页>Alibaba cloud sends SMS verification code
Alibaba cloud sends SMS verification code
2022-07-05 09:05:00 【I want to express myself to the girl I like with code】
1. Buy Alibaba cloud SMS and templates
2. Mobile phone verification code generation
3. Alibaba cloud sends SMS verification code
rely on
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.10</version>
</dependency>
Code ( Copy and use )
Add your own id and secret after
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 {
/**
* Use AK&SK Initialization account 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()
// Your AccessKey ID
.setAccessKeyId("ss")
// Your AccessKey Secret
.setAccessKeySecret("xxx");
// Domain name visited
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
// Generate random number
/**
*
* @param strSource// Add letters and random numbers into the set , Raw random string , You can define
* @param num // Generate a few digit verification code , You can define
* @return
*/
public static String suijiNum(String strSource,int num){
Random random = new Random();
int maxLength=strSource.length()-1;// Random index maximization , Because the index is from 0 Start , So the index of the maximum It's length. -1
String[] strSources = strSource.split("");// The segmentation string is an optional character array , Used for index selection
// The final verification code is spliced ,stringBuffer Create only one object to save memory , And fast , This scenario does not require thread safety
StringBuffer appendStrEnd = new StringBuffer();
for (int i = 0; i < num; i++) {
int suijiIndex = random.nextInt(maxLength);// Build index , Index the position of a character in your original string
String strAlone = strSources[suijiIndex];// Get the single character value in the string
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 Ah, friend ")// Signature name 【 Be careful not to write the name of the template 】
.setTemplateCode("SMS_243991108")// Templates id
.setTemplateParam("{\"code\":\""+code+"\"}");
RuntimeOptions runtime = new RuntimeOptions();
try {
// Please print the copy code yourself API The return value of
client.sendSmsWithOptions(sendSmsRequest, runtime);
} catch (TeaException error) {
// If necessary , Please print error
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// If necessary , Please print error
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
边栏推荐
- [code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
- Codeworks round 639 (Div. 2) cute new problem solution
- Beautiful soup parsing and extracting data
- Halcon snap, get the area and position of coins
- golang 基础 —— golang 向 mysql 插入的时间数据和本地时间不一致
- Install the CPU version of tensorflow+cuda+cudnn (ultra detailed)
- 2011-11-21 training record personal training (III)
- Characteristic Engineering
- [牛客网刷题 Day4] JZ32 从上往下打印二叉树
- 12、动态链接库,dll
猜你喜欢
Halcon affine transformations to regions
Introduction Guide to stereo vision (7): stereo matching
Use and programming method of ros-8 parameters
Halcon Chinese character recognition
IT冷知识(更新ing~)
[technical school] spatial accuracy of binocular stereo vision system: accurate quantitative analysis
Huber Loss
Solutions of ordinary differential equations (2) examples
Applet (subcontracting)
Introduction Guide to stereo vision (4): DLT direct linear transformation of camera calibration [recommended collection]
随机推荐
Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
520 diamond Championship 7-4 7-7 solution
Dynamic dimensions required for input: input, but no shapes were provided. Automatically overriding
【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
Mengxin summary of LCs (longest identical subsequence) topics
MPSoC QSPI Flash 升级办法
Ros-10 roslaunch summary
优先级队列(堆)
多元线性回归(sklearn法)
Kubedm series-00-overview
Codeforces Round #648 (Div. 2) E.Maximum Subsequence Value
How many checks does kubedm series-01-preflight have
Ecmascript6 introduction and environment construction
我从技术到产品经理的几点体会
Halcon clolor_ pieces. Hedv: classifier_ Color recognition
Confusing basic concepts member variables local variables global variables
Illustrated network: what is gateway load balancing protocol GLBP?
资源变现小程序添加折扣充值和折扣影票插件
[daiy4] jz32 print binary tree from top to bottom
2011-11-21 training record personal training (III)