阿里云短信平台发送短信开发

2017/11/25 posted in  网站开发

官方文档:
短信发送API
短信业务规则

一、开发准备

下载SDK
申请秘钥
申请短信签名
添加短信模板

二、.NET版发送短信

需要引入SDK中的aliyun-net-sdk-core.dll和aliyun-net-sdk-dysmsapi.dll

using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;

namespace Whir.SMS.AliYun
{
    public class SendSMS
    {
        private readonly string _sign;
        private readonly string _accessKeyId;
        private readonly string _accessKeySecret;
        /// <summary>
        /// </summary>
        /// <param name="sign">签名</param>
        /// <param name="accessKeyId">Access Key Id</param>
        /// <param name="accessKeySecret">Access Key Secret</param>
        public SendSMS(string sign, string accessKeyId, string accessKeySecret)
        {
            _sign = sign;
            _accessKeyId = accessKeyId;
            _accessKeySecret = accessKeySecret;
        }

        /// <summary>
        /// </summary>
        /// <param name="templateCode">短息模板Code</param>
        /// <param name="phone">手机号</param>
        /// <param name="content">内容键值对,{ 占位符名称:内容 }</param>
        /// <param name="outId">外部流水扩展字段,回执消息中带回</param>
        /// <returns></returns>
        public SendResult Send(string templateCode, string phone, Dictionary<string, string> content, string outId = "")
        {
            String product = "Dysmsapi";//短信API产品名称
            String domain = "dysmsapi.aliyuncs.com";//短信API产品域名
            String accessKeyId = _accessKeyId;//你的accessKeyId
            String accessKeySecret = _accessKeySecret;//你的accessKeySecret

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

            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            try
            {
                //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
                request.PhoneNumbers = phone;
                //必填:短信签名-可在短信控制台中找到
                request.SignName = _sign;
                //必填:短信模板-可在短信控制台中找到
                request.TemplateCode = templateCode;
                //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                request.TemplateParam = content != null && content.Keys.Count > 0 ? content.ToJson() : "";
                //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
                request.OutId = outId;
                //请求失败这里会抛ClientException异常
                SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);

                if (sendSmsResponse.Code.Equals("OK"))
                {
                    return new SendResult
                    {
                        IsSuccess = true,
                        ResponseData = sendSmsResponse.BizId
                    };
                }

                return new SendResult
                {
                    IsSuccess = false,
                    Message = sendSmsResponse.Message
                };
            }
            catch (ServerException e)
            {
                return new SendResult
                {
                    IsSuccess = false,
                    Message = e.Message
                };
            }
            catch (ClientException e)
            {
                return new SendResult
                {
                    IsSuccess = false,
                    Message = e.Message
                };
            }
        }
    }
}