How to send verification code to mobile phone?
I. Preface
Technical name: Ali is greater than SMS verification (one of the ways to send SMS verification is more authoritative).
SMS publishing process:
We usually don't have to write our own code (because we usually have no qualification, if we write our own code, the amount of code will be very large). We need to find a third-party service to write code for us.
Call the third-party service (such as weather service, etc.) and pass the corresponding parameters (such as mobile phone number, etc.). In this case, Ali is more than the service to send SMS verification code.
Supplement:
Alicloud: a cloud collection of servers (hundreds of thousands, millions of machines).
Server: computer with server software installed.
Three networks in one, server cluster, load balancing strategy (load balancer nginx), server downtime, remote disaster recovery strategy.
2, Sending steps of SMS verification code:
(1) Login Ali is greater than: https://dayu.aliyun.com.
(2) Register an alicloud account.
(3) Log in to alicloud account:
(4) Application signature:
(5) Application template:
(7) Phone received verification code:
(9) Get key
(10) Java code implementation:
1) Guide jar package
2) Code:
package test; import java.util.Random; import java.util.Scanner; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest; import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; public class TestPhone_1{ //Note: please do not change the position without modification. public static SendSmsResponse getPhoneMessage(String phoneNumbers,String signName,String templateCode,String templateParam)throws Exception { //Set timeout - self adjustable System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //Several parameters needed to initialize ascClient final String product = "Dysmsapi";//SMS API product name (fixed SMS product name, no need to modify) final String domain = "dysmsapi.aliyuncs.com";//SMS API product domain name (fixed interface address, no need to modify) //Replace with your AK final String accessKeyId = "LTAI0DtxkzU0PmX4";//Your accessKeyId, refer to step 2 of this document final String accessKeySecret = "DloLhsiv0X293YkVHEcIq3dy2TZtqT";//For your accessKeySecret, refer to step 2 of this document //Initialize ascClient. Multiple region s are not supported temporarily (do not modify) IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //Assemble request object SendSmsRequest request = new SendSmsRequest(); //Submit using post request.setMethod(MethodType.POST); //Required: mobile number to be sent. Batch call is supported in the form of comma separation. The maximum batch number is 1000 mobile phone numbers. Batch call is slightly delayed compared to the timeliness of a single call. SMS with verification code type is recommended to use a single call method. When sending international / Hong Kong, Macao and Taiwan messages, the receiving number format is international area code + number, such as "85200000000" request.setPhoneNumbers(phoneNumbers); //Required: SMS signature - can be found in SMS console request.setSignName(signName); //Required: SMS template - can be found in SMS console. When sending international / Hong Kong, Macao and Taiwan messages, please use the international / Hong Kong, Macao and Taiwan SMS template request.setTemplateCode(templateCode); //Optional: the variables in the template replace the JSON string. For example, when the template content is "Dear $, and your verification code is $", the value here is //Friendly tip: if a line break is required in JSON, please refer to the standard JSON protocol for line break requirements. For example, if the SMS content contains a line break, it needs to be expressed as \ \ r\n in JSON. Otherwise, it will cause JSON to fail to parse on the server //"{\"name\":\"Tom\", \"code\":\"888888\"}" request.setTemplateParam(templateParam); //Optional - uplink SMS extension code (the extension code field is controlled at or below 7 digits, please ignore this field for users without special needs) //request.setSmsUpExtendCode("90997"); //Optional: outId is the extension field provided to the business party, which will be brought back to the caller in the SMS receipt message request.setOutId("yourOutId"); //Request failed, ClientException exception will be thrown here SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { //Request successful } return sendSmsResponse; } //Generate random number public static String getRandom(int n){ char[] code = "0123456789".toCharArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; i++) { sb.append(code[new Random().nextInt(code.length)]); } return sb.toString(); } //Pass the corresponding parameters public static String TestSendMessage(String phoneNumbers){ //1. Generate random number and import commons-lang3-3.7.jar package //String random = RandomStringUtils.random(6,"1234567890"); //2. Use random number method to generate random number parameter: number of generated digits String random = getRandom(6); //Store temporarily with scope System.out.println("random number "+random); String name="aaa"; String code = "{\"name\":\""+name+"\", \"code\":\""+random+"\"}"; SendSmsResponse sendSmsResponse=null; String message=null; try { //Parameters: mobile number, SMS signature, SMS template Id, random number in the sent content sendSmsResponse= getPhoneMessage(phoneNumbers,"The heart of the moon","SMS_171112706",code); String messageCode = sendSmsResponse.getCode(); if(messageCode.equals("OK")){ message="Send successfully"; }else{ if(messageCode.equals("isv.INVALID_PARAMETERS")){ message="Parameter exception"; } if(messageCode.equals("isv.AMOUNT_NOT_ENOUGH")){ message="Sorry, your credit is running low"; } } } catch (Exception e) { e.printStackTrace(); } return message; } //test public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String toTmobile = scanner.next(); // For debugging //String message=TestSendMessage("13933863662"); String message=TestSendMessage(toTmobile); System.out.println(message); } }Rain in the Yanxue river of Beiyi Published 39 original articles, won praise 0, visited 204 Private letter follow