首页 > 代码库 > C#批量发送手机短信——云通讯平台
C#批量发送手机短信——云通讯平台
云通讯平台:http://www.yuntongxun.com
1、在云通讯平台建短信模板
2、通过上述账号信息编辑如下:
//phoneNumber 发送到的手机号 content 短信内容
public ResponseMessage SmsSend(string phoneNumber, string content) { CCPRestSDK api = new CCPRestSDK(); //ip格式如下,不带https:// bool isInit = api.init("app.cloopen.com", "8883"); api.setAccount("主账号ACCOUNT SID", "主账号AUTH TOKEN"); api.setAppId("应用AppId"); string ret; ResponseMessage result = new ResponseMessage() { Status = false, Msg = "发送失败" }; try { if (isInit) { string[] data =http://www.mamicode.com/ { content }; Dictionary<string, object> retData = http://www.mamicode.com/api.SendTemplateSMS(phoneNumber, "短信模板号", data); ret = getDictionaryData(retData); if (retData["statusCode"].ToString() == "000000") { result = new ResponseMessage() { Status = true, Msg = "发送成功" }; } else { result = new ResponseMessage() { Status = false, Msg = "发送失败" }; } } else { result = new ResponseMessage() { Status = false, Msg = "发送失败" }; } } catch (Exception exc) { ret = exc.Message; result = new ResponseMessage() { Status = false, Msg = exc.Message }; } return result; }
public class ResponseMessage { /// <summary> /// 操作类型(teaCRMEnums.ActionEnum的字符串形式) /// </summary> public string Action { get; set; } /// <summary> /// 返回状态 /// </summary> public bool Status { get; set; } /// <summary> /// 返回结果 /// </summary> public string Result { get; set; } /// <summary> /// 自定义提示信息 /// </summary> public string Msg { get; set; } }
/// <summary> /// 模板短信 /// </summary> /// <param name="to">短信接收端手机号码集合,用英文逗号分开,每批发送的手机号数量不得超过100个</param> /// <param name="templateId">模板Id</param> /// <param name="data">可选字段 内容数据,用于替换模板中{序号}</param> /// <exception cref="ArgumentNullException">参数不能为空</exception> /// <exception cref="Exception"></exception> /// <returns></returns> public Dictionary<string, object> SendTemplateSMS(string to, string templateId, string[] data) { Dictionary<string, object> initError = paramCheckRest(); if (initError != null) { return initError; } initError = paramCheckMainAccount(); if (initError != null) { return initError; } initError = paramCheckAppId(); if (initError != null) { return initError; } if (to == null) { throw new ArgumentNullException("to"); } if (templateId == null) { throw new ArgumentNullException("templateId"); } try { string date = DateTime.Now.ToString("yyyyMMddhhmmss"); // 构建URL内容 string sigstr = MD5Encrypt(m_mainAccount + m_mainToken + date); string uriStr = string.Format("https://{0}:{1}/{2}/Accounts/{3}/SMS/TemplateSMS?sig={4}", m_restAddress, m_restPort, softVer, m_mainAccount, sigstr); Uri address = new Uri(uriStr); WriteLog("SendTemplateSMS url = " + uriStr); // 创建网络请求 HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest; setCertificateValidationCallBack(); // 构建Head request.Method = "POST"; Encoding myEncoding = Encoding.GetEncoding("utf-8"); byte[] myByte = myEncoding.GetBytes(m_mainAccount + ":" + date); string authStr = Convert.ToBase64String(myByte); request.Headers.Add("Authorization", authStr); // 构建Body StringBuilder bodyData = http://www.mamicode.com/new StringBuilder(); if (m_bodyType == EBodyType.EType_XML) { request.Accept = "application/xml"; request.ContentType = "application/xml;charset=utf-8"; bodyData.Append("<?xml version=‘1.0‘ encoding=‘utf-8‘?><TemplateSMS>"); bodyData.Append("<to>").Append(to).Append("</to>"); bodyData.Append("<appId>").Append(m_appId).Append("</appId>"); bodyData.Append("<templateId>").Append(templateId).Append("</templateId>"); if (data != null && data.Length > 0) { bodyData.Append("<datas>"); foreach (string item in data) { bodyData.Append("<data>").Append(item).Append("</data>"); } bodyData.Append("</datas>"); } bodyData.Append("</TemplateSMS>"); } else { request.Accept = "application/json"; request.ContentType = "application/json;charset=utf-8"; bodyData.Append("{"); bodyData.Append("\"to\":\"").Append(to).Append("\""); bodyData.Append(",\"appId\":\"").Append(m_appId).Append("\""); bodyData.Append(",\"templateId\":\"").Append(templateId).Append("\""); if (data != null && data.Length > 0) { bodyData.Append(",\"datas\":["); int index = 0; foreach (string item in data) { if (index == 0) { bodyData.Append("\"" + item + "\""); } else { bodyData.Append(",\"" + item + "\""); } index++; } bodyData.Append("]"); } bodyData.Append("}"); } byte[] byteData =http://www.mamicode.com/ UTF8Encoding.UTF8.GetBytes(bodyData.ToString()); WriteLog("SendTemplateSMS requestBody = " + bodyData.ToString()); // 开始请求 using (Stream postStream = request.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } // 获取请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { // Get the response stream StreamReader reader = new StreamReader(response.GetResponseStream()); string responseStr = reader.ReadToEnd(); WriteLog("SendTemplateSMS responseBody = " + responseStr); if (responseStr != null && responseStr.Length > 0) { Dictionary<string, object> responseResult = new Dictionary<string, object> { { "statusCode", "0" }, { "statusMsg", "成功" }, { "data", null } }; if (m_bodyType == EBodyType.EType_XML) { XmlDocument resultXml = new XmlDocument(); resultXml.LoadXml(responseStr); XmlNodeList nodeList = resultXml.SelectSingleNode("Response").ChildNodes; foreach (XmlNode item in nodeList) { if (item.Name == "statusCode") { responseResult["statusCode"] = item.InnerText; } else if (item.Name == "statusMsg") { responseResult["statusMsg"] = item.InnerText; } else if (item.Name == "TemplateSMS") { Dictionary<string, object> retData = http://www.mamicode.com/new Dictionary<string, object>(); foreach (XmlNode subItem in item.ChildNodes) { retData.Add(subItem.Name, subItem.InnerText); } responseResult["data"] = new Dictionary<string, object> { { item.Name, retData } }; } } } else { responseResult.Clear(); responseResult["resposeBody"] = responseStr; } return responseResult; } return new Dictionary<string, object> { { "statusCode", 172002 }, { "statusMsg", "无返回" }, { "data", null } }; } } catch (Exception e) { throw e; } }
private string getDictionaryData(Dictionary<string, object> data) { string ret = null; foreach (KeyValuePair<string, object> item in data) { if (item.Value != null && item.Value.GetType() == typeof(Dictionary<string, object>)) { ret += item.Key.ToString() + "={"; ret += getDictionaryData((Dictionary<string, object>)item.Value); ret += "};"; } else { ret += item.Key.ToString() + "=" + (item.Value =http://www.mamicode.com/= null ? "null" : item.Value.ToString()) + ";"; } } return ret; }
C#批量发送手机短信——云通讯平台
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。