NET 阿里大于短信推送

来源:互联网 发布:联合证券股票交易软件 编辑:程序博客网 时间:2024/05/21 06:24

最近再做一个商城,需要短信推送,研究了下阿里大于的短信推送。

注册,登陆啥的 就不说了 。注册好像分商家和个人。(这里吐槽下。商家真是幸福,短信模板都建好了,个人的居然毛也没有!)

注册成功以后需要建一个应用(需要审核 ),然后建短信模板(需要审核),然后就可以愉快的测试了 。

下载一个SDK文件, 然后添加到自己的工程里面。(这些都是废话)

因为官方给的例子 里面 只有测试的样本 ,所以这里自己封装了下,参数读取配置文件

 /// <summary>        /// 发送短信        /// </summary>        /// <param name="destination">电话号码</param>        /// <param name="text">阿里的签名,短信参数-参数值-参数-参数值,模板编码</param>         /// <returns></returns>        private string SendMessage(string destination, string text, string speed = "0")        {            if (!string.IsNullOrWhiteSpace(destination))            {                MessageSMSConfig config = SMSCore.GetConfig();                 string[] textArr = text.Split(new char[] { ',' });                string strQM = textArr[0];//签名                string strCSARR = textArr[1];//参数信息                string strMB = textArr[2];//模板代码                //参数信息格式化:{参数:'参数值',参数:'参数值'......}                string[] strcs = strCSARR.Split(new char[] { '-' });                string scs = "{";                for (int i = 0; i < strcs.Length; i++)                {                    if (i % 2 == 0)                    {                        scs = scs + strcs[i] + ":";                    }                    else                    {                        scs = scs + "'" + strcs[i] + "',";                    }                }                scs = scs.Substring(0, scs.Length - 1)+"}";                ITopClient client = new DefaultTopClient(AlidayuUrl, config.AppKey, config.AppSecret);                AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();                req.Extend = "";//好像没用,暂时舍弃                req.SmsType = "normal";                req.SmsFreeSignName = strQM;                req.SmsParam = scs;                 req.RecNum = destination;                req.SmsTemplateCode = strMB;                Log.Info(client == null);                try                {                    AlibabaAliqinFcSmsNumSendResponse rsp = client.Execute(req);                    //Console.WriteLine(rsp.Body);                    if (rsp.Result.Success)                    {                         return "发送成功";                    }                    else {                        return rsp.Result.Msg;                    }                }                catch (Exception ex)                {                    Log.Error(ex, ex);                    return ex.Message;                }                                /**/            }            return "发送目标不能为空!";        }


0 0
原创粉丝点击