阿里云发送短信过程中遇到的坑

1.登录阿里云,再阿里云首页搜索进入短信服务控制台

阿里云发送短信过程中遇到的坑

 

2 在控制台中添加摸板与签名

阿里云发送短信过程中遇到的坑

阿里云发送短信过程中遇到的坑

 

注意: 签名不要写名字,具体参考签名参考规范

3开启AccessKey

阿里云发送短信过程中遇到的坑

 

记录里面的值

阿里云发送短信过程中遇到的坑

 

4 现已获取

AccessKeySecret 开发者秘钥对
signName 短信签名
templateCode  短信模板
AccessKeyId 开发者秘钥对

 

5 编写帮助类

public class SendSMS 
    {
        private static string domain="dysmsapi.aliyuncs.com" ;
        //产品域名
        private static string product ="Dysmsapi";

     
        private static string endpointName = "cn-beijing";
        private static string regionId = "cn-beijing";
        /// <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 ResultObject<Dictionary<SendInformation, SendSmsResponse>> SendSms( string phoneNumber, string templateParam,string   AccessKeyId, string AccessKeySecret, string signName, string templateCode)
        {
            ResultObject<Dictionary<SendInformation, SendSmsResponse>> result = new ResultObject<Dictionary<SendInformation, SendSmsResponse>>() { Message="",Success=false};
            Dictionary<SendInformation, SendSmsResponse> Info = new Dictionary<SendInformation, SendSmsResponse>();
            try
            {
               
                //templateParam = "{\"code\":\"小李\"}";
                var Id = Guid.NewGuid();
                var outId = Id.ToString();
                IClientProfile profile = DefaultProfile.GetProfile(regionId, AccessKeyId, AccessKeySecret);
                DefaultProfile.AddEndpoint(endpointName, regionId, product, domain);
                IAcsClient acsClient = new DefaultAcsClient(profile);

//该类为了记录发送日志(可不写)
                SendInformation Information = new SendInformation()
                {
                    Id = Guid.NewGuid(),
                    accessKeyId = AccessKeyId,
                    accessKeySecret = AccessKeySecret,
                    endpointName = endpointName,
                    outId = outId,
                    phoneNumber = phoneNumber,
                    regionId = regionId,
                    signName = signName,
                    State = 1,
                    templateCode = templateCode,
                    templateParam = templateParam,
                    Message = "",
                    CreateTime=DateTime.Now
                };
                // 组装请求对象
                SendSmsRequest request = new SendSmsRequest
                {
                    PhoneNumbers = phoneNumber,
                    SignName = signName,
                    TemplateCode = templateCode,
                    TemplateParam = templateParam,
                    OutId = outId
                };
                SendSmsResponse response = null;
                try
                {
                    // 请求失败这里会抛ClientException异常
                    response = acsClient.GetAcsResponse(request);
                    if (response.Code == "OK")
                    {
                        result.Success = true;
                        Information.State = 0;
                    }
                    else
                    {
                        result.Message = response.Message;
                        Information.Message = response.Message;
                    }
                }
                catch (ServerException e)
                {
                    // LogHelper.LogException<ServerException>(e.ErrorMessage);
                    Information.Message = e.Message;
                    Information.State = 1;
                    result.Message = e.Message;
                }
                catch (ClientException e)
                {
                    // LogHelper.LogException<ClientException>(e.ErrorMessage);
                    Information.Message = e.Message;
                    Information.State = 1;
                    result.Message = e.Message;
                }
                Info.Add(Information, response);
                result.Data = Info;
            }
            catch (Exception e)
            {
                result.Message = e.Message;
            }
            return result;
        }
    }

该类需要引用

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

dll 文件获取方式

1https://help.aliyun.com/document_detail/112145.html?spm=a2c4g.11186623.2.16.4bcd19d9sgWFau

 

6调用帮助类

 Code = "{\"code\":\""+ Code + "\"}";

 

 var Result = SendSMS.SendSms(Tel, Code, AccessKeyId, AccessKeySecret, signName, templateCode);