C#使用云片发送短信_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C#使用云片发送短信

C#使用云片发送短信

 2017/11/11 21:03:30  小谭朋友  程序员俱乐部  我要评论(0)
  • 摘要:一、概述:因为目前在做的实名制项目,其中有的模块会用到发送短信的功能,所以尝试着使用了云片短信服务平台发送短信,,然后在这里记录一下,防备忘记!二、下载文档和SDK:这里默认已经在云片平台上进行了注册,得到了ApiKey,也对短信签名和短信模板进行了报备!所以这里直接记录一下直接使用官方提供的SDK以及在其基础上进行融合。官方说明文档地址:https://www.yunpian.com/doc/zh_CN/introduction/download.htmlC#SDK的Git地址:https
  • 标签:C# 使用 发送短信

  一、概述:

    因为目前在做的实名制项目,其中有的模块会用到发送短信的功能,所以尝试着使用了 云片短信服务平台 发送短信,,然后在这里记录一下,防备忘记!

  

  二、下载文档和SDK:

    这里默认已经在云片平台上进行了注册,得到了ApiKey,也对短信签名和短信模板进行了报备!所以这里直接记录一下直接使用官方提供的SDK以及在其基础上进行融合

    官方说明文档地址:https://www.yunpian.com/doc/zh_CN/introduction/download.html

    C# SDK的Git地址:https://github.com/yunpian/yunpian-net-sdk

 

  三、编译SDK:

    从Git上下载了SDK后解压得到一个文件夹,使用VS打开SDK文件(我使用的是VS2017)。

  

    项目打开后,如果直接使用SDK中的API进行发送,则只需要直接对项目进行编译,然后在Yunpian\bin\Debug下得到一个Yunpian.dll文件,

    

  四、调用DLL:

    1、在自己的项目中右键点击引用 —》选择添加引用 —》点击预览 —》转到Yunpian.dll所在文件夹 —》选择添加。

    2、配置好从云片服务平台得到的ApiKey。

    2、官方SDK中的调用示例:

    class="code_img_closed" src="/Upload/Images/2017111121/0015B68B3C38AA5B.gif" alt="">
  1 using System;
  2 using System.Collections.Generic;
  3 using Yunpian.conf;
  4 using Yunpian.lib;
  5 using Yunpian.model;
  6 using System.Web;
  7 using System.Text;
  8 namespace YunpianSDKTest
  9 {
 10     class Program
 11     {
 12     /**
 13      * 更多内容请参考 <url>https://www.yunpian.com/api2.0/howto.html</url>
 14      */
 15 
 16     /**
 17      *
 18      * 如您第一次使用云片网,强烈推荐先看云片网络设置教程 <url>https://blog.yunpian.com/?p=94</url>
 19      *
 20      * 使用说明
 21      *
 22      * 1、登陆 <url>http://www.yunpian.com/</url> 获取APIKEY
 23      * 2、使用APIKEY生成Config
 24      * 3、获取需要的操作类SmsOperator/UserOperator/TplOperator/FlowOperator/VoiceOperator
 25      * 4、通过Result来接收返回值.具体可参考示例
 26      *
 27      * 返回值参考
 28      * <url>https://www.yunpian.com/api2.0/sms.html</url>
 29      * <url>https://www.yunpian.com/api2.0/record.html</url>
 30      */
 31         static void Main(string[] args)
 32         {
 33             //设置apikey
 34             Config config = new Config("your apikey");
 35             Dictionary<string, string> data = new Dictionary<string, string>();
 36             Result result = null;
 37 
 38             // 获取用户信息
 39             UserOperator user = new UserOperator(config);
 40             result = user.get(data);
 41             Console.WriteLine(result);
 42 
 43             // 获取模板信息
 44             TplOperator tpl = new TplOperator(config);
 45             data.Clear();
 46             data.Add("tpl_id", "1");
 47             result = tpl.getDefault(data);
 48             Console.WriteLine(result);
 49 
 50 
 51             // 发送单条短信
 52             SmsOperator sms = new SmsOperator(config);
 53             data.Clear();
 54             data.Add("mobile", "14012341234");
 55             data.Add("text", "【yunpian】您的验证码是9981");
 56             result = sms.singleSend(data);
 57             Console.WriteLine(result);
 58 
 59             // (这个是模板接口发送,会因为一些特殊字符产生编码问题导致发送失败,不推荐使用)
 60             //data.Clear();
 61             //string tpl_value = HttpUtility.UrlEncode(
 62             //HttpUtility.UrlEncode("#code#", Encoding.UTF8) + "=" +
 63             //HttpUtility.UrlEncode("1234", Encoding.UTF8) + "&" +
 64             //HttpUtility.UrlEncode("#company#", Encoding.UTF8) + "=" +
 65             //HttpUtility.UrlEncode("云片网", Encoding.UTF8), Encoding.UTF8);
 66             //data.Add("mobile", "14012341231,123,13012312312");
 67             //data.Add("tpl_value", tpl_value);
 68             //data.Add("tpl_id", "1");
 69             //result = sms.tplSingleSend(data);
 70             //Console.WriteLine(result);
 71 
 72             // (批量发送的接口耗时比单号码发送长,如果需要更高并发速度,推荐使用single_send/tpl_single_send)
 73             //data.Clear();
 74             //data.Add("mobile", "14012341231,123,13012312312");
 75             //data.Add("text", "【yunpian】您的验证码是9981");
 76             //result = sms.batchSend(data);
 77             //Console.WriteLine(result);
 78             // 发送个性化短信
 79             //data.Clear();
 80             //data.Add("mobile", "14022341231,123,13112312312,13112312312");
 81             //data.Add("text", "【yunpian】您的验证码是9981,【yunpian】您的验证码是1981,【yunpian】您的验证码是9921,【yunpian】您的验证码是9981");
 82             //result = sms.multiSend(data);
 83             //Console.WriteLine(result);
 84 
 85             // 发送语音
 86             // VoiceOperator voice = new VoiceOperator(config);
 87             // data.Clear();
 88             // data.Add("code", "1421");
 89             // data.Add("mobile", "13012312312");
 90             // result = voice.send(data);
 91             // Console.WriteLine(result);
 92 
 93             // 发送流量
 94             // FlowOperator flow = new FlowOperator(config);
 95             // data.Clear();
 96             // result = flow.getPackage(data);
 97             // Console.WriteLine(result);
 98 
 99             // data.Clear();
100             // data.Add("sn", "1008601");
101             // data.Add("mobile", "18712341234");
102             // result = flow.recharge(data);
103             // Console.WriteLine(result);
104             // Console.ReadLine();
105 
106         }
107     }
108 }
logs_code_collapse">View Code

    3、我自己进行了一个简单的封装:

    
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using Yunpian.conf;
  7 using Yunpian.lib;
  8 using Yunpian.model;
  9 
 10 namespace Yunpian.sms
 11 {
 12     public class YPSMS
 13     {
 14         /// 配置属性
 15         private  string apikey;
 16         private string apiSecret;
 17         private Dictionary<string, string> data = new Dictionary<string, string>();
 18         private Result result = null;
 19         private Config config = null;
 20 
 21         public YPSMS(string apikey, string apiSecret)
 22         {
 23             this.apikey = apikey;
 24             this.apiSecret = apiSecret;
 25             this.config = new Config(apikey, apiSecret);
 26         }
 27         public YPSMS(string apikey)
 28         {
 29             this.apikey = apikey;
 30             this.apiSecret = null;
 31             this.config = new Config(apikey);
 32         }
 33 
 34         /// <summary>
 35         /// 获取账户信息
 36         /// </summary>
 37         /// <returns></returns>
 38         public Result GetAccountInfo()
 39         {
 40             UserOperator user = new UserOperator(config);
 41             result = user.get(data);
 42             return result;
 43         }
 44 
 45         /// <summary>
 46         /// 根据模板id获取模板内容
 47         /// </summary>
 48         /// <param name="id"></param>
 49         /// <returns></returns>
 50         public Result GetMsgModelById(string id)
 51         {
 52             // 获取模板信息
 53             TplOperator tpl = new TplOperator(config);
 54             data.Clear();
 55             data.Add("tpl_id", id);
 56             result = tpl.getDefault(data);
 57             return result;
 58         }
 59 
 60         /// <summary>
 61         /// 发送单条短信
 62         /// </summary>
 63         /// <param name="toPhone">接收方电话号码</param>
 64         /// <param name="msgModel">使用的消息模板</param>
 65         /// <returns></returns>
 66         public Result SendSMS(string toPhone,string msgModel)
 67         {
 68             SmsOperator sms = new SmsOperator(config);
 69             data.Clear();
 70             data.Add("mobile", toPhone);
 71             data.Add("text", msgModel);
 72             result = sms.singleSend(data);
 73             return result;
 74         }
 75 
 76         /// <summary>
 77         /// 使用统一模板,批量发送短信
 78         /// </summary>
 79         /// <param name="listPhone">批量发送接收电话</param>
 80         /// <param name="msgModel">短信模板</param>
 81         /// <returns>发送结果</returns>
 82         public Result SendSMS(List<string> listPhone,string msgModel)
 83         {
 84             String toPhone = String.Empty;
 85             foreach (string phone in listPhone)
 86             {
 87                 if (toPhone.Length > 0) toPhone += ",";
 88 
 89                 toPhone += phone;
 90             }
 91 
 92             SmsOperator sms = new SmsOperator(config);
 93             data.Clear();
 94             data.Add("mobile", toPhone);
 95             data.Add("text", msgModel);
 96             result = sms.batchSend(data);
 97             return result;
 98         }
 99 
100         /// <summary>
101         /// 发送个性化短信
102         /// </summary>
103         /// <param name="listPhone"></param>
104         /// <param name="listMsgModel"></param>
105         /// <returns></returns>
106         public Result SendSMS(List<string> listPhone, List<string> listMsgModel)
107         {
108             String toPhone = String.Empty; // 格式: "phone,phone,phone,phone"
109             String msgModel = String.Empty; // 格式:"model,model,model,model"
110 
111             foreach (string phone in listPhone)
112             {
113                 if (toPhone.Length > 0) toPhone += ",";
114 
115                 toPhone += phone;
116             }
117 
118             foreach (string model in listMsgModel)
119             {
120                 if (msgModel.Length > 0) msgModel += ",";
121 
122                 msgModel += model;
123             }
124 
125             SmsOperator sms = new SmsOperator(config);
126             data.Clear();
127             data.Add("mobile", toPhone);
128             data.Add("text", msgModel);
129             result = sms.multiSend(data);
130             return result;
131         }
132     }
133 }
View Code

    调用方式:

    
 1 using System;
 2 using System.Collections.Generic;
 3 using Yunpian.model;
 4 using Yunpian.sms;
 5 
 6 namespace YunpianSDKTest
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             YPSMS SMS = new YPSMS("ce2xxxxxxxxxxxxxxx00bb1");
13             String MsgModel = "【xxxx】您好:xxxxx,请尽快登录系统。";
14             Result result = null;
15 
16             /// 获取账户信息
17             result = SMS.GetAccountInfo();
18             Console.WriteLine(result);
19 
20             /// 发送一条短信
21             result = SMS.SendSMS("183xxxxx278", MsgModel);
22             Console.WriteLine(result);
23 
24             /// 根据ID获取短信模板
25             result = SMS.GetMsgModelById("1");
26             Console.WriteLine(result);
27 
28             /// 批量发送短信
29             List<string> phone = new List<string>();
30             phone.Add("183xxxxx278");
31             phone.Add("151xxxxx327");
32 
33             result = SMS.SendSMS(phone, MsgModel);
34             Console.WriteLine(result);
35 
36             /// 发送个性化短信,针对不同的人使用不同的模板,但是必须一一对应
37             List<string> model = new List<string>();
38             model.Add(MsgModel);
39             model.Add(MsgModel);
40 
41             result = SMS.SendSMS(phone, model);
42             Console.WriteLine(result);
43 
44             Console.ReadLine();
45 
46         }
47     }
48 }
View Code

 

上一篇: Vue.js—组件快速入门及Vue路由实例应用 下一篇: 没有下一篇了!
发表评论
用户名: 匿名