[.NET]阿里云SMS发送短信与记录查询

项目中需要添加以下依赖项,在控制台中执行安装依赖项:

Install-Package aliyun-net-sdk-core
Install-Package aliyun-net-sdk-dysmsapi

以下是封装的发送短信以及查询发送详情的方法:

public class SmsApi
{
    //云通信短信API产品名称
    private const string product = "Dysmsapi";
    //产品域名
    private const string domain = "dysmsapi.aliyuncs.com";

    /// <summary>
    /// 发送短信
    /// </summary>
    /// <param name="accessKeyId">开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理</param>
    /// <param name="accessKeySecret">开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理</param>
    /// <param name="phoneNumber">待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式</param>
    /// <param name="signName">短信签名,可在短信控制台中找到,例如 云通信</param>
    /// <param name="templateCode">短信模板,可在短信控制台中找到,例如 SMS_1000000</param>
    /// <param name="templateParam">模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为,例如 {\"name\":\"小李\",\"code\":\"546523\"}</param>
    /// <param name="outId">提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者</param>
    /// <param name="endpointName">端点名称</param>
    /// <param name="regionId">地区名称</param>
    /// <returns></returns>
    public static SendSmsResponse SendSms(string accessKeyId, string accessKeySecret, string phoneNumber, string signName, string templateCode, string templateParam = "", string outId = "", string endpointName = "cn-hangzhou", string regionId = "cn-hangzhou")
    {
        IClientProfile profile = DefaultProfile.GetProfile(regionId, accessKeyId, accessKeySecret);
        DefaultProfile.AddEndpoint(endpointName, regionId, product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        // 组装请求对象
        SendSmsRequest request = new SendSmsRequest
        {
            PhoneNumbers = phoneNumber,
            SignName = signName,
            TemplateCode = templateCode,
            TemplateParam = templateParam,
            OutId = outId
        };
        SendSmsResponse response = null;
        try
        {
            // 请求失败这里会抛ClientException异常
            response = acsClient.GetAcsResponse(request);
        }
        catch (ServerException e)
        {
            LogHelper.LogException<ServerException>(e.ErrorMessage);
        }
        catch (ClientException e)
        {
            LogHelper.LogException<ClientException>(e.ErrorMessage);
        }
        return response;
    }

    /// <summary>
    /// 查询发送详情
    /// </summary>
    /// <param name="accessKeyId">开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理</param>
    /// <param name="accessKeySecret">开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理</param>
    /// <param name="phoneNumber">手机号</param>
    /// <param name="sendDate">发送日期 支持30天内记录查询</param>
    /// <param name="pageSize">页大小</param>
    /// <param name="currentPage">当前页码,从1开始计数</param>
    /// <param name="bizId">流水号</param>
    /// <param name="endpointName">端点名称</param>
    /// <param name="regionId">地区名称</param>
    /// <returns></returns>
    public static QuerySendDetailsResponse QuerySendDetails(string accessKeyId, string accessKeySecret, string phoneNumber, DateTime sendDate, long? pageSize, long? currentPage, string bizId = "", string endpointName = "cn-hangzhou", string regionId = "cn-hangzhou")
    {
        // 初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.GetProfile(regionId, accessKeyId, accessKeySecret);
        DefaultProfile.AddEndpoint(endpointName, regionId, product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        // 组装请求对象
        QuerySendDetailsRequest request = new QuerySendDetailsRequest
        {
            PhoneNumber = phoneNumber,
            BizId = bizId,
            SendDate = sendDate.ToString("yyyyMMdd"),
            PageSize = pageSize,
            CurrentPage = currentPage
        };
        QuerySendDetailsResponse querySendDetailsResponse = null;
        try
        {
            // 请求失败这里会抛ClientException异常
            querySendDetailsResponse = acsClient.GetAcsResponse(request);
        }
        catch (ServerException e)
        {
            LogHelper.LogException<ServerException>(e.ErrorMessage);
        }
        catch (ClientException e)
        {
            LogHelper.LogException<ClientException>(e.ErrorMessage);
        }
        return querySendDetailsResponse;
    }
}

调用发送短信:

var accessKeyId = "";
var accessKeySecret = "";
var phone = "13511111111";
var signName = "阿里云";
var templateCode = "SMS_155175208";
var validateCode = "123456";
var templateParam = "{\"code\":\"" + validateCode + "\"}";
SmsApi.SendSms(accessKeyId, accessKeySecret, phone, signName, templateCode, templateParam);

原文地址:https://www.jianshu.com/p/9780884f4bb5

posted @ 2019-02-16 17:02  huayueniansi  阅读(847)  评论(0编辑  收藏  举报