SMS verification package


java.util.Random import;

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.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.Date;

com.aliyuncs.DefaultAcsClient Import;
Import com.aliyuncs.IAcsClient;
Import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
Import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
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 {
// product name: cloud communications SMS API product, developers do not need to replace
static Final product String = "Dysmsapi";
// product domain, developers do not need to replace
static final String domain = "dysmsapi.aliyuncs.com ";

// TODO here need to replace the developer's own AK (looking at the console access Ali cloud)
static accessKeyId Final String = "LTAI4FbpKBxAgDzf4wbuAgQW"; // use before the pilot package
static final String accessKeySecret = "ZVYmOuN9SsPwKBJbOARnGFqab72c3h" ;

public static SendSmsResponse sendSms(String phone, String code) throws ClientException {

// can self-adjust the timeout
System.setProperty ( "sun.net.client.defaultConnectTimeout", "10000");
System.setProperty ( "sun.net.client.defaultReadTimeout", "10000");

// Initialization acsClient, does not support region of
IClientProfile Profile = DefaultProfile.getProfile ( "CN-Hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint ( "CN-Hangzhou", "CN-Hangzhou", Product, Domain);
IAcsClient acsClient = new DefaultAcsClient (profile);

// Assembly request object - particularly described in the console - parts of the documentation
SendSmsRequest Request new new SendSmsRequest = ();
// Required: phone number to be sent
request.setPhoneNumbers (Phone);
// Required: SMS signature - can SMS console find
request.setSignName ( "superior products purchased");
// required: SMS templates - can be found in the SMS console
request.setTemplateCode ( "SMS_175538375");
// optional: template variable substitution JSON string, such as the template reads "your verification code is $ {code}" ..., here is
request.setTemplateParam ( "{\" code \ ": \" "+ code +" \ "}");

// Optional - up message spreading code (no special needs of users ignore this field)
//request.setSmsUpExtendCode("90997 ");

// Optional: outId is provided to the business side extension field, and ultimately back to the message receipt message this value to the caller
request.setOutId ( "yourOutId");

// hint here might throw an exception, note the catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse (Request);

sendSmsResponse return;
}

public static genUniqueKey the synchronized String () {
the Random Random new new = the Random ();
Integer Number = random.nextInt (900000) + 1000;
return String.valueOf (Number);
}

public static void main (String [] args ) {
the try {
String code = SmsUtils.genUniqueKey ();
SmsUtils.sendSms ( "fill in the phone number to be transmitted", code);
System.out.println ( "send SMS successful verification code:" + code);
} the catch ( E ClientException) {
e.printStackTrace ();
}
}
}

Guess you like

Origin www.cnblogs.com/linlangtianshang/p/11792650.html