阿里云短信服务

一、去阿里云官网申请短信服务(在官网里申请,这里不详述)

二、申请密钥(AccessKey ID和AccessKey Secret,在官网里申请,这里不详述)

二、调用API(java版本,代码是官方示例代码的改良,更加直观明确,代码中有相应的注释)

短信相关API文档地址:https://next.api.aliyun.com/document/Dysmsapi/2017-05-25/overview

1、发送短信,

(1)首先需要获取几个基本参数,密钥AccessKey ID和AccessKey Secret、签名名称、模板code以及发送对象的手机号

 

(2)构建Client连接对象

(3)发送短信

API发送对象的参数详细参考官方文档:

https://next.api.aliyun.com/document/Dysmsapi/2017-05-25/SendSms?spm=api-workbench.API%20Document.0.0.1cca1e0fJSQDHI

(4)发送后获取相应的返回码判断是否发送成功

返回码列表详细参考官方文档:

https://help.aliyun.com/document_detail/101346.html?spm=api-workbench.API%20Document.0.0.3a281e0flGZueo

2、查询发送状态

查询发送状态需要几个基本参数,回执id(短信发送成功后,在返回响应对象中会返回,详情查看发送API文档)以及发送的手机号。

发送后获取相应的返回码判断是否发送成功

返回详细信息参考官方文档:

https://next.api.aliyun.com/document/Dysmsapi/2017-05-25/QuerySendDetails?spm=api-workbench.API%20Document.0.0.1cca1e0fJSQDHI

最后附上完整代码:

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.QuerySendDetailsRequest;
import com.aliyun.dysmsapi20170525.models.QuerySendDetailsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;

public class Sample {

    // 签名名称
    private static String signName = "xxx";

    // 模板code
    private static String templateCode = "xxx";

    // AccessKey ID
    private static String ACCESS_KEY_ID = "xxx";

    //     AccessKey Secret
    private static String ACCESS_KEY_SECRET = "xxx";

    /**
     * 创建Client连接对象
     * @param accessKeyId
     * @param accessKeySecret
     * @return
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config();
        config.accessKeyId = accessKeyId;
        config.accessKeySecret = accessKeySecret;
        return new Client(config);
    }

    /**
     * 发送短信
     * @param phoneNumber
     * @return
     * @throws Exception
     */
    public static SendSmsResponse sendSms(String phoneNumber) throws Exception {

        // 创建连接
        Client client = createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);

        // 封装发送对象
        SendSmsRequest sendReq = new SendSmsRequest()
                .setPhoneNumbers(phoneNumber)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam("{\"code\":\"1111\"}");

        // 发送短信
        SendSmsResponse sendResp = client.sendSms(sendReq);

        return sendResp;
    }

    /**
     * 通过手机号和发送成功返回的响应对象中的回执id查询发送状态
     * @param phoneNumber   手机号
     * @param bizId 发送成功返回的响应对象中的回执id
     * @return
     * @throws Exception
     */
    public static QuerySendDetailsResponse querySendDetails(String phoneNumber, String bizId) throws Exception {

        // 创建连接
        Client client = createClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET);

        // 构建查询对象
        QuerySendDetailsRequest queryReq = new QuerySendDetailsRequest()
                .setPhoneNumber(Common.assertAsString(phoneNumber))
                .setBizId(bizId)
                .setSendDate(com.aliyun.darabonbatime.Client.format("yyyyMMdd"))
                .setPageSize(10L)
                .setCurrentPage(1L);

        // 调用查询API
        QuerySendDetailsResponse queryResp = client.querySendDetails(queryReq);

        return queryResp;
    }

    public static void main(String[] args_) throws Exception {

        String phoneNumber = "xxxx";

        // -----------发送短信-----------

        SendSmsResponse sendResp = sendSms(phoneNumber);

        // 获取响应返回码
        String code = sendResp.body.code;

        System.out.println("code: " + code + ", message: " + sendResp.body.message);

        // 如果发送成功
        if (Common.equalString(code, "OK")) {

            // -----------查询短信发送结果,发送成功则查询-----------

            // 等待10秒后查询结果
            Common.sleep(10000);

            // 获取发送响应对象中的回执id
            String bizId = sendResp.body.bizId;

            // 查询结果
            QuerySendDetailsResponse queryResp = querySendDetails(phoneNumber, bizId);

            // 获取查询结果响应返回码
            String queryCode = queryResp.body.code;

            System.out.println("code: " + queryCode + ", message: " + queryResp.body.message);
        }

    }
}

ps:需要完整的工程项目的先点个赞然后私信我。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值