using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Text.RegularExpressions; //红色命名空间引用是必须的,分别是端口、线程和正则表达式
/// <summary>
///SMS 的摘要说明
/// </summary>
public class SMS
{
public SMS()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 将汉字转换为Unicode
/// </summary>
/// <param name="text">要转换的字符串</param>
/// <returns></returns>
public static string GBToUnicode(string text)
{
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(text);
string lowCode = "", temp = "";
for (int i = 0; i < bytes.Length; i++)
{
if (i % 2 == 0)
{
temp = System.Convert.ToString(bytes[i], 16);//取出元素4编码内容(两位16进制)
if (temp.Length < 2) temp = "0" + temp;
}
else
string mytemp = Convert.ToString(bytes[i], 16);
if (mytemp.Length < 2) mytemp = "0" + mytemp; lowCode = lowCode + mytemp + temp;//取出元素4编码内容(两位16进制)
}
return lowCode;
#region 发送短息
/// <summary>
/// SendMessage函数用于发送短信.参数strMobileNumber设置手机号码,strMsgContent指
/// 定要发送的内容
/// </summary>
/// <param name="strMobileNumber">手机号码</param>
/// <param name="strMsgContent">要发送的内容</param>
/// <returns> 返回指示发送是否成功</returns>
public bool SendMessage(string strMobileNumber,string strMsgContent)
//查看机器可用的COM端口,如果COM1已经被占用的话,请选择其他端口
SerialPort serialPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); //端口的一些参数
serialPort.WriteTimeout = 2000;
try
serialPort.ReadBufferSize = 900000;
if (serialPort.IsOpen) serialPort.Close();
serialPort.Open();
serialPort.WriteLine("ATZ/r");
Thread.Sleep(500);
serialPort.ReadExisting();
serialPort.WriteLine("AT+CSMP=1,167,0,8" + "/r"); //短息消息格式
serialPort.Write("AT+CMGS=" + strMobileNumber + "/r"); //发送短信AT指令
Thread.Sleep(1000);
string content = GBToUnicode(strMsgContent);
serialPort.Write(content + Convert.ToChar(26).ToString()); //Ctrl+Z的ASCII编码值
Thread.Sleep(2000);
string msg = serialPort.ReadExisting();
serialPort.Close();
return true;
catch (Exception ex)
Console.WriteLine(ex.Message);
return false;
finally
//保证操作完成以后关闭端口,避免占用
if (serialPort.IsOpen)
serialPort.Close();
#endregion
}
前台页面aspx:
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="30" align="right" bgcolor="#f2f2f2" class="left_txt2">
手机号码:
</td>
<td height="30" bgcolor="#f2f2f2">
<asp:TextBox ID="tbPhoneNum" runat="server"></asp:TextBox>
&nbsp;
<td height="30" bgcolor="#f2f2f2" class="left_txt">
</tr>
短信内容:
<asp:TextBox ID="tbContent" runat="server" Height="69px" TextMode="MultiLine" Width="303px"></asp:TextBox>
<td colspan="3" align="center">
<table width="100%" height="31" border="0" cellpadding="0" cellspacing="0" class="nowtable">
<tr>
<td class="left_bt2">
<asp:Button ID="btnDX" runat="server" Text="发送短信" OnClick="btnDX_Click" />
</td>
</tr>
</table>
</table>
#region 发送短信
protected void btnSend_Click(object sender, EventArgs e)
{
//获取手机号码与短信内容
string cSendPhoneNum = tbPhoneNum.Text.Trim();
string cSendContent = tbContent.Text.Trim();
#region 利用短信猫发送短信
SMS sms = new SMS();
try
//正则表达式验证,验证手机号码是否格式正确
string code = @"^(13[0-9]|15[0|3|6|7|8|9]|18[8|9])/d{8}$";
if (Regex.IsMatch(cSendPhoneNum, code))
//对短信的长度进行控制
if (cSendContent.Length == 0||cSendContent.Length > 200)
{
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('短信内容为空或太长!')</script>");
return ;
}
//手机号码格式正确
if (sms.SendMessage(cSendPhoneNum, cSendContent))
//短信发送成功
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('短信发送成功...'); </script>");
else
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('短信发送失败...'); </script>");
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('手机号码格式错误,请检查...')</script>");
catch (Exception ex)
ClientScript.RegisterClientScriptBlock(GetType(), "", "<script>alert('"+ex.Message+"')</script>");
#endregion
以上就是全部可用代码,并加以适当注释。
以下附注常用的AT指令表:
FUNCTIONS
AT COMMANDS
DETAILS
选择短消息服务
AT+CSMS
选择是否打开短消息服务以及广播服务
短消息存储
AT+CPMS
选择短消息优先存储区域
短消息格式
AT+CMGF
选择短消息支持格式(TEXT or PDU)
保存设置
AT+CSAS
保存+CSCA and +CSMP参数设置
恢复设置
AT+CRES
恢复+CSCA and +CSMP参数设置
显示TEXT参数
AT+CSDH
显示当前TEXT模式下结果代码
新消息提示
AT+CNMI
选择当有新的短消息来时系统提示方式
读短消息
AT+CMGR
读取短消息
列短消息
AT+CMGL
将存储的短消息列表
发送短消息
AT+CMGS
写短消息
AT+CMGW
写短消息并保存在存储器中
从内存中发短消息
AT+CMSS
发送在存储器中保存的短消息
设置TEXT参数
AT+CSMP
设置在TEXT模式下条件参数
删除短消息
AT+CMGD
删除保存的短消息
服务中心地址
AT+CSCA
提供短消息服务中心的号码
选择广播类型
AT+CSCB
选择系统广播短消息的类型
广播标识符
AT+WCBM
读取SIM卡中系统广播标识符
短消息位置修改
AT+WMSC
修改短消息位置
短消息覆盖
AT+WMGO
写一条短消息放在第一个空位