1.首先要找到合适的短信的接口。
2.确定给谁发短信和短信内容。
1 /// <summary> 2 /// 发短信 3 /// </summary> 4 /// <param name="tel">电话</param> 5 6 private void SendMessageToRequestor(string tel) 7 { 8 string url = string.Empty;//短信接口路径 9 string content = string.Empty;//短信内容,可增加变量来替换短信内容 比如:content = content.Replace("${ItCode}", itcode);//itcode 10 try 11 { 12 url = url.Replace("${message}", HttpUtility.UrlEncode(content, Encoding.GetEncoding("GBK"))); 13 url = url.Replace("${mobile}", tel);//收短信人的电话号码 14 this.HttpGet(url); 15 } 16 catch (Exception ex) 17 { 18 19 } 20 } 21 22 /// <summary> 23 /// Get 24 /// </summary> 25 /// <param name="url"></param> 26 /// <param name="queryStr"></param> 27 /// <returns></returns> 28 public string HttpGet(string url) 29 { 30 HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url); 31 //myReq.ContentType = "application/x-www-form-urlencoded"; 32 myReq.Method = "GET"; 33 myReq.Headers.Add("Accept-Encoding", "utf-8"); 34 HttpWebResponse HttpWResp; 35 try 36 { 37 HttpWResp = (HttpWebResponse)myReq.GetResponse(); 38 } 39 catch (WebException ex) 40 { 41 HttpWResp = (HttpWebResponse)ex.Response; 42 } 43 Stream myStream = HttpWResp.GetResponseStream(); 44 StreamReader reader = new StreamReader(myStream, Encoding.UTF8); 45 string content = reader.ReadToEnd(); 46 reader.Close(); 47 HttpWResp.Close(); 48 return content; 49 50 }
发送短信要调用外部的接口来进行,并在数据库配置相应的短信内容。这只是系统中的最基本的方法之一,message 代表的是邮见内容,里面可以增加变量
(因为我用的接口是公司自己买的短信接口,所以也不方便透露)