最近工作,一直在做发送短信的功能,当系系统终端收到消息,如果管理员没有在电脑旁边,这时候收到一条消息,要及时通知其他人做出处理,所有该项目必须具备发送短信的功能。
我们的发送短信的功能是独立的一个模块,配置发送短信的电话、短信内容等,接收消息,发送消息。要发送短信的电话号码和短信的模板在配置文件中写好。如下我写了一个小例子,我要给自己发一封如下的消息:
姓名:赵亚盟 消息类型:邀请函 消息内容:2016年1月1号万达广场参加元旦晚会,具体通知请看邮件!
通知时间:2015-12-26 09:55:28.
配置文件中配置相关信息:
// 配置文件config中配置电话和短信内容模板
<configSections>
<sectionGroup name="SMS.Manager">
<section name="TEL" type="System.Configuration.NameValueSectionHandler"/>
<section name="MSG" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<SMS.Manager>
<TEL>
<!--增加的电话号码用"|"隔开-->
<add key="Test" value="18832639**3|1368158***6"/>
</TEL>
<MSG>
<add key="MSGTest" value="姓名:{0} 消息类型:{1} 消息内容:{2} 通知时间:{3}."/>
</MSG>
</SMS.Manager>
这里只是写了一个小例子,所有写了一些假数据
static void Main(string[] args)
{
try
{
ISMSTest iSMSTest = new SMSTest();
SMSEntity entity = new SMSEntity();
entity.Id = Guid.NewGuid().ToString();
entity.Name = "赵亚盟";
entity.Type = "邀请函";
entity.Content = "2016年1月1号万达广场参加元旦晚会,具体通知请看邮件!";
entity.Time=DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
iSMSTest.Update(entity);
Console.WriteLine(entity);
}
catch (Exception)
{
throw;
}
}
实体:
public class SMSEntity
{
/// <summary>
/// 消息ID
/// </summary>
public string Id
{ get; set; }
/// <summary>
/// 消息类型
/// </summary>
public string Type
{ get; set; }
/// <summary>
/// 姓名
/// </summary>
public string Name
{ get; set; }
/// <summary>
/// 消息内容
/// </summary>
public string Content
{ get; set; }
/// <summary>
/// 通知时间
/// </summary>
public string Time
{ get; set; }
}
接口:
public interface ISMSTest
{
void Update(SMSEntity smsEntity);
}
实现类:
public class SMSTest:ISMSTest
{
public void Update(SMSEntity smsEntity)
{
if (smsEntity == null)
{
return;
}
Appconfig.Init(); //初始化配置文件
SMSDataOut SendMessage = new SMSDataOut(); //发送短信
string[] tel = Appconfig.htTEL["Test"].ToString().Split('|'); //获取要发送短信的电话号码
foreach (string str in tel) //遍历要发送短信的所以电话号码
{
SendMessage.SendSMSMessage(smsEntity, str);
}
}
}
初始化配置文件,读取要发送短信的电话号码和短信模板;
public class Appconfig
{
public static Hashtable htTEL = new Hashtable(); //电话
public static Hashtable htMSG = new Hashtable(); //短信模板
private static bool InitConfigflag = false; //设置标志位
public static void Init()
{
if (InitConfigflag)
{
return;
}
else
{
//加载配置文件
NameValueCollection nc = (NameValueCollection)ConfigurationManager.GetSection("SMS.Manager/TEL");
for (int i = 0; i < nc.AllKeys.Length; i++)
{
//装载电话号码
htTEL.Add(nc.AllKeys[i], nc[i]);
}
//加载配置文件
nc = (NameValueCollection)ConfigurationManager.GetSection("SMS.Manager/MSG");
for (int i = 0; i < nc.AllKeys.Length; i++)
{
//装载短信模板
htMSG.Add(nc.AllKeys[i], nc[i]);
}
InitConfigflag = true;
}
}
}
获取短信内容:
public class SMSDataOut
{
/// <summary>
/// 发送短信
/// </summary>
/// <param name="smsEntity">短信实体</param>
/// <param name="telNo">电话号码</param>
/// <returns></returns>
public bool SendSMSMessage(SMSEntity smsEntity, string telNo)
{
if (smsEntity == null || string.IsNullOrEmpty(telNo))
return false;
//获取短信内容
string msg = GetSMSMessage(smsEntity);
////构建短信实体
MSG_OUTBOX mo = DataOutDrivers.CreateNewOutBox(telNo, msg);
////发送短信
DataOutDrivers.AddSMSBox(mo);
return true;
}
/// <summary>
/// 获取短信
/// </summary>
/// <param name="messageEntity"></param>
/// <returns></returns>
public string GetSMSMessage(SMSEntity smsEntity)
{
string messageInfo="";
messageInfo = string.Format(Appconfig.htMSG["MSGTest"].ToString(),
smsEntity.Name,
smsEntity.Type,
smsEntity.Content,
smsEntity.Time
);
//短信信息内容
return messageInfo;
}
构建短信实体:
/// <summary>
/// 创建短信
/// </summary>
/// <param name="telNo">电话号码</param>
/// <param name="msg">短信内容</param>
/// <returns></returns>
public static MSG_OUTBOX CreateNewOutBox(string telNo, string msg)
{
MSG_OUTBOX newMo = new MSG_OUTBOX();
newMo.EXPRESSLEVEL = 2;//发送级别,系统分为3个级别: 0 为最高优先级1 较高优先级 2 普通优先级
newMo.MSGTYPE = 0;//0= 普通短信 1 = 彩信 (系统默认为1,彩信)2 = wap push3 = 免提短信(快闪短信)
newMo.RECEIVER = telNo;
newMo.MSGTITLE = msg;
return newMo;
}
发送短信:
public static void AddSMSBox(MSG_OUTBOX outmsg)
{
//发送结果
/bool bRet = ServiceFactory.GetMSGData().AddMSGOUTBOX(outmsg);
}
另外配备了一个短信猫,我们要发送的短信内容和电话都存入了数据库中,短信猫直接读取数据库,将短信发送出去。
手机叮叮一响,短信收到。完工。