.net调用阿里云短信接口

由于阿里云短信接口有两类:
一类是:
AppKey ====
AppSecret ====
AppCode ====
二类是:
AccetKey ====
AccetSecret ====
根据需求申请相应的key和secret;
这里我使用的是第二类,千万不要把一类和二类搞混淆,这样就没法正确的进行短信发送了。
短信接口调用分为两类,一类是简单api发送短信,二类是sdk引用发送接口
SDK:skd下载链接
api:api调用方式

这里我使用的是SDK形式,推荐使用api调用方式

//首先准备好你的accetkey、accetserct、SignName、TempCode
//分别是key、serct、签名、模板编号可参考相关手册
//将下载下来的SDK包中的dll文件放到你项目的bin目录中
//右键点击相对应的应用程序添加引用
public static void SmsSend(string NumPhone, string validateNum)
        {
            string singName = ConfigurationManager.AppSettings["SignName"];//你的签名
            string tempCode = ConfigurationManager.AppSettings["TempCode"];//你的模板编号
            string accetKey = ConfigurationManager.AppSettings["AccetKey"];//你的key
            string accetSerct = ConfigurationManager.AppSettings["accetserct"];//你的accetserct
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accetKey, accetSerct);
            IAcsClient client = new DefaultAcsClient(profile);
            SingleSendSmsRequest request = new SingleSendSmsRequest();
            try
            {
                request.SignName = singName;
                request.TemplateCode = tempCode;
                request.RecNum = NumPhone;
                request.ParamString = "{\"code\":\"" + validateNum + "\"}";//模板code中定义的参数,可以定义多个,这里我只定义了一个code;validateNum是传进来的验证码,在后端随机生成,可自行处理
                SingleSendSmsResponse httpResponse = client.GetAcsResponse(request);
            }
            catch (ServerException ex)
            {
                string result = ex.Message;
            }
            catch (ClientException ez)
            {
                string result = ez.Message;
            }
        }

调用的话,传入相应的手机号,和验证码既可以完成短信发送,此处只是简略调通短信的方式,安全处理需自行加入处理。
申请短信的方式,阿里云官网有详细讲解,可参照

猜你喜欢

转载自blog.csdn.net/qq_31975127/article/details/53765910