C#使用阿里云平台模拟发送短信消息(验证码)

想要使用阿里云平台的短信服务,首先要注册一个阿里云账号,由于发送短信消息需要用到短信签名、短信模板ID(已添加并通过审核)

1、注册阿里云账号(地址:https://www.aliyun.com/)

   注册账号成功后,可以查看账号的AccessKey(密钥),概览-->Accesskey

 2、添加短信签名和短信模板

登录阿里云账号后,找到管理控制台,搜索短信服务,左边菜单找到国内信息,添加签名和模板

1)添加短信签名

 2)添加短信模板

 3、短信服务相关引用dll

1)aliyun-net-sdk-core.dll(实现短信服务发送的dll)

2)Newtonsoft.Json.dll(实现方法的返回对象引用的dll)

 4、编写发送短信的源代码

1)config添加如下配置

    <!-- 阿里云平台短信服务的授权ID、授权密钥 -->
    <add key="aliyun.AccessKeyId" value="LTAI4GKbPJhAbs1b5wEFrY4CGb"/>
    <add key="aliyun.AccessKeySecret" value="2vGPH6ZFa2Gr4VBzcGv5356R6VZu1f1"/>

2) 添加SmsSendHeper 帮助类

  /// <summary>
    /// 短信发送辅助类
    /// </summary>
    public class SMSSendHelper
    {      
        #region 阿里云平台短信服务

        //获取配置文件的授权ID、授权密钥
        public static string aliAccessKeyId = ConfigurationManager.AppSettings["aliyun.AccessKeyId"];
        public static string aliAccessKeySecret = ConfigurationManager.AppSettings["aliyun.AccessKeySecret"];

        /// <summary>
        /// 发送手机短信验证码(阿里云平台)
        /// </summary>
        /// <param name="mobile">接收手机</param>
        /// <param name="signName">短信签名</param>
        /// <param name="tempCode">短信模板ID</param>
        /// <param name="mobileCode">短信验证码</param>
        /// <returns></returns>
        public static bool SendMobileMessageByAli(string mobile,string signName,string tempCode,string mobileCode)
        {
            //拼接读短信目标变量值  "{\"code\":\"1234\"}"
            string tempParam = "{'code"+"':"+ mobileCode+"}";
            //发送短信并返回结果
            JObject result = SendMessageByAliSms(aliAccessKeyId, aliAccessKeySecret, mobile,signName,tempCode, tempParam);
            //返回代码
            if (result["Code"].ToString().ToLower() == "ok")
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 发送手机短信信息(通过阿里云SDK)
        /// </summary>
        /// <param name="aliAccessKey">授权id(阿里云账号的AccessKeyId )</param>
        /// <param name="aliAccessSecret">授权密钥(阿里云账号的AccessKeySecret)</param>
        /// <param name="phoneNumber">接收手机号(多个用逗号隔开)</param>
        /// <param name="signName">短信签名</param>
        /// <param name="tempCode">短信模板ID</param>
        /// <param name="tempParam">短信模板变量</param>
        /// <returns></returns>
        public static JObject SendMessageByAliSms(string aliAccessKey, string aliAccessSecret,string phoneNumber, string signName, string tempCode, string tempParam)
        {
            //返回结果对象
            JObject result = new JObject();

            //接口文档(里面有sdk):https://help.aliyun.com/product/44282.html?spm=5176.12226155.0.0.33ac1cbeMfrIwkhttps://home.firefoxchina.cn           
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", aliAccessKey, aliAccessSecret);   //cn-hangzhou: 默认节点
            DefaultAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.Method = MethodType.POST;  //请求方式
            request.Domain = "dysmsapi.aliyuncs.com";  //请求域名
            request.Version = "2017-05-25";  //请求版本
            request.Action = "SendSms";  //请求操作
            request.AddQueryParameters("PhoneNumbers", phoneNumber); 
            request.AddQueryParameters("SignName", signName);
            request.AddQueryParameters("TemplateCode", tempCode);
            request.AddQueryParameters("TemplateParam", tempParam);
            try
            {
                //发送请求请返回响应
                CommonResponse response = client.GetCommonResponse(request);
                //获取返回的内容
                string content = Encoding.Default.GetString(response.HttpResponse.Content);
                //序列化成对象(Code: 请求状态码,Message:状态码描述,BizId:发送回执ID, RequestId: 请求ID)
                result = (JObject)JsonConvert.DeserializeObject(content);

            }
            catch (ServerException e)
            {
                result.Add("Code","500");
                result.Add("Message","短信服务异常:"+ e.Message);
            }
            catch (ClientException e)
            {
                result.Add("Code", "501");
                result.Add("Message", "客户端异常:"+ e.Message);
            }
            return result;
        }

        #endregion

    }

5、调用发送短信帮助类方法

  //测试发送短信验证码(阿里云)
  bool isSuccess = SMSSendHelper.SendMobileMessageByAli("13822074174", "珠海德加云", "SMS_189485372","321234");

6、收到短信验证码

 参考网站:

https://www.cnblogs.com/shanfeng1000/p/11751315.html
https://blog.csdn.net/weixin_44484756/article/details/106180736

posted @ 2021-10-25 15:30  小小邪  阅读(933)  评论(0编辑  收藏  举报