阿里云短信服务申请

进入阿里云的短信服务添加签名和模板

https://cdn.jsdelivr.net/gh/xWangHZ/CDN@master/img/Java_%E9%98%BF%E9%87%8C%E4%BA%91_%E7%9F%AD%E4%BF%A1/1.png

https://cdn.jsdelivr.net/gh/xWangHZ/CDN@master/img/Java_%E9%98%BF%E9%87%8C%E4%BA%91_%E7%9F%AD%E4%BF%A1/1.png

创建阿里云API的密钥

https://cdn.jsdelivr.net/gh/xWangHZ/CDN@master/img/Java_%E9%98%BF%E9%87%8C%E4%BA%91_%E7%9F%AD%E4%BF%A1/1.png

在maven上添加依赖

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.0.6</version> 
    <!-- 注:如提示报错,先升级基础包版,无法解决可联系技术支持 -->
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
    <version>1.1.0</version>
</dependency>

Java代码

定义常量

final String product = "Dysmsapi";
//短信API产品名称(短信产品名固定,无需修改)
final String domain = "dysmsapi.aliyuncs.com";
//短信API产品域名(接口地址固定,无需修改)

final String accessKeyId = "yourAccessKeyId";
//你的accessKeyId,参考本文档步骤2
final String accessKeySecret = "yourAccessKeySecret";
//你的accessKeySecret,参考本文档步骤2

初始化ascClient

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId, accessKeySecret);

IAcsClient client = new DefaultAcsClient(profile);

CommonRequest request = new CommonRequest();

定义随机数

//这个步骤是根据个人需要可有可无

public class RandTest {
    private  String num = null;
    public RandTest() {
        for (int i=0;i<8;i++){
            int r = (int)(Math.random()*(10-1));
            if(i==0){
                num = String.valueOf(r);
            }
            else{
                num+=r;
            }
            System.out.println(num);
        }
    }

    public String getNum() {
        return num;
    }
}

构建请求

request.putQueryParameter("SignName","你的签名名称");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", "要发送的手机号");
request.putQueryParameter("SendDate", "日期");
request.putQueryParameter("TemplateCode","模板CODE");
request.putQueryParameter("TemplateParam", "{\"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;

public class Main{
    public static void main(String[] args) {

        final String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改)
        final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改)

        final String accessKeyId = "yourAccessKeyId";
        //你的accessKeyId,参考本文档步骤2
        final String accessKeySecret = "yourAccessKeySecret";
        //你的accessKeySecret,参考本文档步骤2

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();

        RandTest randTest = new RandTest();
        String num = randTest.getNum();

        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("SignName","你的签名名称");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "要发送的手机号");
        request.putQueryParameter("SendDate", "日期");
        request.putQueryParameter("TemplateCode","模板CODE");
        request.putQueryParameter("TemplateParam", "{\"code\":\""+num+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

一只小菜鸡