SMS development API ( used when only part of the third step to modify the winning red )
1. Import coordinate maven
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.0.0</version> </dependency>
2. Tools forming short codes: ValidateCodeUtils
Package com.itheima.health.mobile.utils; Import java.util.Random; / ** * @date: the Created in 2019/6/11 * @description: randomly generated codes tools * @version : 1.0 * / / ** * tools randomly generated codes * / public class ValidateCodeUtils { / ** * randomly generated codes * @param length length of 4 or 6 * @return * / public static Integer generateValidateCode ( int length) { Integer code = null ; IF (length ==. 4 ) { code = new new the Random () the nextInt (9999);. // generates a random number, a maximum of 9999 IF (code <1000 ) { code = code 1000 +; // ensure that the random number is four digits } } the else IF (length = . 6 = ) { code = new new the random () the nextInt (999999);. // generates a random number, a maximum of 999999 IF (code <100000 ) { code = 100000 + code; // ensure that the random number is 6 digits } } the else { the throw new newRuntimeException ( "generate only four or six digit code verification" ); } return code; } / ** * randomly generated codes specify the length of the string * @param length length * @return * / public static String generateValidateCode4String ( int length) { the Random RDM = new new the Random (); String hash1 = Integer.toHexString (rdm.nextInt ()); String capstr = hash1.substring (0 , length); return capstr; } }
3. Send SMS codes tools: SMSUtils
package com.itheima.health.mobile.utils; /** * @date :Created in 2019/6/11 * @description : * @version: 1.0 */ 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.exceptions.ClientException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; /** * 短信发送工具类 */ public class SMSUtils { public static Final String VALIDATE_CODE = " the SMS_ into your own "; // send SMS verification code public static Final String ORDER_NOTICE = " the SMS_ into your own "; // examination reservation success notification / * * * send SMS * @param phoneNumbers * @param param * @throws ClientException * / public static void sendShortMessage (phoneNumbers String, String param) throws ClientException { System.setProperty ( "sun.net.client.defaultConnectTimeout", "10000" ); System.setProperty ( "sun.net.client.defaultReadTimeout", "10000" ); // initialize ascClient need a few parameters Final String Product = "Dysmsapi"; // SMS API product name (product name fixed SMS, without modification) Final String domain = "dysmsapi.aliyuncs.com"; // SMS API product domain name (interface address fixed, without modification) Final String accessKeyId = " into your own "; // your accessKeyId, Final String accessKeySecret = " into your own ";// your accessKeySecret, // initialization ascClient, does not support multi-region (do not modify) Profile = DefaultProfile.getProfile IClientProfile ( "CN-Hangzhou" , accessKeyId, accessKeySecret); DefaultProfile.addEndpoint ( "CN-Hangzhou", "CN-Hangzhou" , Product, Domain); IAcsClient acsClient = new new DefaultAcsClient (Profile); // assembly request object SendSmsRequest = request new new SendSmsRequest (); // use the post submission request.setMethod (MethodType.POST); // required: phone number to be transmitted. Support batch call to comma-delimited form, the upper limit is 1000 batch cell phone number, the call volume with respect to a single call to the timeliness of a slight delay, the type of message authentication code is recommended to use a single call request.setPhoneNumbers (phoneNumbers); // required: SMS signature - can be found in the SMS console request.setSignName ( " change their message signatures " ); // Required: SMS templates - can be found in the SMS console request.setTemplateCode (VALIDATE_CODE); // Optional: template variable substitution JSON string, such as templates reads "Dear $ {name}, your verification code is $ {code}", the value is here // tips: If you need to JSON with line breaks, please refer to the standard JSON protocol for line breaks requirements, such as the content of the message contains \ r \ n in the case of need expressed in JSON \\ r \\ n, otherwise it will lead to failure in the server parse JSON IF (param! = null && param.length ()> 0 ) { request.setTemplateParam ( "{\" code \ ": \" "+ + param" \ "}" ); } SendSmsResponse sendSmsResponse = acsClient.getAcsResponse (Request); the System.out.println("SendSms Result:"+sendSmsResponse.getMessage()); if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { // 请求成功 System.out.println("请求成功"); } } public static void main(String args[]) throws Exception{ String validateCode = ValidateCodeUtils.generateValidateCode4String(4); System.out.println("validateCode:"+validateCode); SMSUtils.sendShortMessage("手机号码",validateCode); } }