Send SMS verification code to use Ali cloud messaging service

Ali cloud service uses short hair process:

  1. complete the purchase SMS services on Ali cloud .

  2. Related introducing a jar.

        <-! Ali cloud messaging service ->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.0</version>
        </dependency>
        <-! Ali cloud messaging service ->

  

  3. Copy the following template code ( to complete the acquisition parameters 3.1 and 3.2 can be used ) .

    3.1 SMS service purchased from the acquisition parameters: signName (signature name) , templateCode (stencil CODE) , <accessKeyId> , <accessSecret> ;

      Note: The first two arguments apply for the position ( press the need to choose domestic or international ):

 

 

      The last two parameters acquired positions ( <accessKeyId> , <accessSecret> ):

 

    3.2 Incoming parameters: Phone (telephone number), code (to be sent to the user authentication code).

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/**
 * Use Ali cloud messaging service to send SMS verification code
 * / 
Public  class SendSms {
     / **
     * @Title: sendSMS
     * @Description: Ali cloud send text messages
     * @Param   Phone phone number
     * @Param   code custom codes
     * @Param   signature name created on the SMS service signName Ali cloud
     * @Param   template templateCode Ali cloud messaging service created templates CODE
     * @return void
     */
    public static void sendSMS(String phone,String code,String signName,String templateCode){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        // splice template parameter (authentication code) value 
        String codeSMS = "{\" code \ ": \" "+ code +" \ "}" ;

        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 ( "SignName", signName ); // signature must be created on the signature name Ali cloud messaging services 
        request.putQueryParameter ( "TemplateCode", templateCode ); // template must be created using text messages on Ali cloud services templates template CODE 
        request.putQueryParameter ( "PhoneNumbers", phone ); // phone number 
        request.putQueryParameter ( "TemplateParam" , codeSMS);
         the try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace ();
        } catch (ClientException e) {
            e.printStackTrace ();
        }
    }
  
}

Guess you like

Origin www.cnblogs.com/wanghj-15/p/12167986.html