Alibaba SMS service in java (with random SMS verification code generation class) --Rookie Xiaohui

Ali SMS service in java (SMS verification code)

1. Create a signature

  • Create a signature in the following location and wait for approval
    enter description here
    enter description here

2. Create a template

  • Create a template in the following location and wait for approval
    enter description here
    enter description here

3. Test the verification code function

  • Fill in the relevant information and test the sending.
    enter description here
    enter description here
    Note: The sending failed may be because your balance is insufficient. You can choose to purchase a package. For new numbers, try the location shown below to receive free SMS.
  • Official website homepage drop down last
  • enter description here
  • enter description here

4. View Api Demo

  • enter description here
  • enter description here

5. Get AK information

  • enter description here
  • Create AccessKey
  • enter description here
  • Save the AccessKeyId and AccessKeySecret
  • enter description here
  • Copy it and fill in the positions of AccessKeyId and AccessKeySecret respectively
  • enter description here

6. Copy Api Demo to eclipse; copy dependencies to pom.xml

enter description here

7. Self-built tools

//六位随机密码生成
//传入手机号,成功返回后台随机验证码,失败返回null
public class ToolNote {
	
	public static String getNote(String tel){
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "你的accesskeyId", "你的secret");
        IAcsClient client = new DefaultAcsClient(profile);

        String[] codees={"0","1","2","3","4","5","6","7","8","9"};
        String code="";
        for(int i=0;i<6;i++){
        	int j=(int)(Math.random()*10);
        	code+=codees[j];
        }
        
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("SignName", "IDSE");
        request.putQueryParameter("TemplateCode", "SMS_175540528");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        CommonResponse response=null;
        try {
        	response= client.getCommonResponse(request);
            System.out.println(response.getData());
            //获取当前返回字符串转换为json对象
            JSONObject json_object=JSONObject.parseObject(response.getData());
            //根据键获得值看是否为“OK”,是则发送成功,返回验证码
            String status=(String)json_object.get("Message");
            if(status.equals("OK")){
            	return code;
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        
        return null;
	}
}

Guess you like

Origin blog.csdn.net/qq_39231769/article/details/102692076