Ali Cloud SMS --JAVA

    1. label templates and applications

        Ali cloud to be logged in the official website:  https://www.aliyun.com/    then search SMS, go.

 On the left navigation bar to select domestic or international news

            

  Each signature template and then apply for one.

  After waiting patiently by the application can be used by the template signature name and CODE are the following need to use.

  Prior to this there is one thing, give an account to charge money (red test if one dollar is enough).

 

    2. Send SMS Tools

      First, the introduction of the first jar package, Maven following coordinates:

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.4</version>
        </dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
            <version>1.1.0</version>
        </dependency>

  Note: The version to be consistent, or two jar package have chosen the latest, otherwise version conflicts.

       Tools:

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
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.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class SmsUtils {

    //开发者的ID
    private static final String ACCESS_KEY_ID = "xxxxxxxxxxxxxxxxxxx";
    //开发者的秘钥
    private static final String ACCESS_SECRET = "xxxxxxxxxxxxxxxxxxx";
    //申请的短信模板代码
    private static final String TEMPLATE_CODE = "SMS_173474008";
    //申请的标签名称
    private static final String SIGN_NAME = "xuye";
    //产品名称:云通信短信API产品,开发者无需替换
    private static final String PRODUCT = "Dysmsapi";
    //产品域名,开发者无需替换
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";


    /**
     *
     * @param phoneNumbers 接收短信的手机号 国内11位
     * @param param 模板中的变量替换JSON字符串,如模板内容为"您的验证码为${code}",则此字符串中的code值便是替换该内容
     * @throws ClientException
     */
    public static void sendShortMessage(String phoneNumbers,String param) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("default", ACCESS_KEY_ID, ACCESS_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "default");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("TemplateCode",TEMPLATE_CODE);
        request.putQueryParameter("TemplateParam", param);
        request.putQueryParameter("SignName",SIGN_NAME);
        try {
            CommonResponse response = client.getCommonResponse(request);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

     The code has been tested tools like copy and paste.

     Developer keys to move to the head, choose accesskeys point to go to.

     

Published 35 original articles · won praise 61 · views 50000 +

Guess you like

Origin blog.csdn.net/m0_37914588/article/details/100586021