使用阿里云实现短信发送服务(测试版)

官网发送测试短信步骤
官网短信测试注册步骤

第一步

注册阿里云账号

第二步

注册成功之后
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

第三步

获取AK
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

阿里云官网提供的原生API(验证码发送)

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.dysmsapi.model.v20170525.*;

public class SendSms {

    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your-access-key-id>", "<your-access-key-secret>");
        /** use STS Token
        DefaultProfile profile = DefaultProfile.getProfile(
            "<your-region-id>",           // The region ID
            "<your-access-key-id>",       // The AccessKey ID of the RAM account
            "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
            "<your-sts-token>");          // STS Token
        **/
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");
        request.setTemplateCode("SMS_154950909");
        request.setPhoneNumbers("18291015512");
        request.setTemplateParam("{\"code\":\"1234\"}");

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
}

使用的时候需要完善API,代码如下

固定格式下的API
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.dysmsapi.model.v20170525.*;

public class SendSms {

    public static void main(String[] args) {

        /*①引入初始化ascClient需要的几个参数,
        格式固定,不需要改变,粘贴赋值即可*/
	final String product = "Dysmsapi";// 短信API产品名称(短信产品名固定,无需修改)
	final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名(接口地址固定,无需修改)

        /*②修改参数,
        将<your-access-key-id>和<your-access-key-secret>替换位自己的,
        region ID根据自己的地区选择*/
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<your-access-key-id>", "<your-access-key-secret>");
        /** use STS Token
        DefaultProfile profile = DefaultProfile.getProfile(
            "<your-region-id>",           // The region ID
            "<your-access-key-id>",       // The AccessKey ID of the RAM account
            "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
            "<your-sts-token>");          // STS Token
        **/
        //③引入下面代码,固定
          DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
          
        IAcsClient client = new DefaultAcsClient(profile);

        SendSmsRequest request = new SendSmsRequest();
        //④在测试中,只可以是阿里云短信测试,不可以修改
        request.setSignName("阿里云短信测试");
        //⑤修改为自己的模板Code
        request.setTemplateCode("SMS_153950927");
        //⑥修改为自己的手机号
        request.setPhoneNumbers("xxxxxx");
        //⑦测试专用验证码1234
        request.setTemplateParam("{\"code\":\"1234\"}");

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
}


定义工具类生成验证码
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.dysmsapi.model.v20170525.*;

		//①定义了一个工具类SMSUtils,实现短信发送功能,方便在其他地方调用
public class SMSUtils {

		//②定义了一个方法sendShortMessage:需要传入电话号码(自己固定也可)和验证码(通过工具类生成,自己定义都可以)
    public static void sendShortMessage(String telephone,String code) throws ClientException {

        // ③初始化ascClient需要的几个参数(固定写法,粘贴复制即可)
	final String product = "Dysmsapi";// 短信API产品名称(短信产品名固定,无需修改)
	final String domain = "dysmsapi.aliyuncs.com";// 短信API产品域名(接口地址固定,无需修改)

	   //④输入The region ID、The AccessKey ID of the RAM account、The AccessKey Secret of the RAM account(在自己的阿里云中申请AccessKey,修改值)
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "xxxx", "xxxx");

		//⑤引入下列代码,固定
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/
        IAcsClient client = new DefaultAcsClient(profile);


		//⑥测试中setSignName只能是阿里云短信测试不可修改,即唯一测试专用签名
		//setTemplateCode是自己对应的模板Code
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("阿里云短信测试");
        //⑦修改为自己的模板Code
        request.setTemplateCode("SMS_153950927");
        //⑧使用传入的telephone,作为要接收发送的手机号
        request.setPhoneNumbers(telephone);
        //⑨使用自己随机生成的验证码
        request.setTemplateParam("{\"code\":\""+code+"\"}");


        try {
        //调用是否成功,是否会出现错误,状态码---错误信息
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
        //打印出出错信息
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
}

版权声明:本文为weixin_46931860原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。