当前位置:网站首页>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);
        }
    }



}

原网站

版权声明
本文为[I want to express myself to the girl I like with code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050902284866.html