简单的发短信,难不倒程序员

本文介绍了如何在工作中实现发送短信功能,该功能作为独立模块,包含配置短信电话、内容等步骤。通过配置文件设置电话号码和短信模板,利用短信猫从数据库读取信息并发送,成功实现了消息实时通知。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        最近工作,一直在做发送短信的功能,当系系统终端收到消息,如果管理员没有在电脑旁边,这时候收到一条消息,要及时通知其他人做出处理,所有该项目必须具备发送短信的功能。

        我们的发送短信的功能是独立的一个模块,配置发送短信的电话、短信内容等,接收消息,发送消息。要发送短信的电话号码和短信的模板在配置文件中写好。如下我写了一个小例子,我要给自己发一封如下的消息: 

       姓名:赵亚盟 消息类型:邀请函 消息内容: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);

        }



另外配备了一个短信猫,我们要发送的短信内容和电话都存入了数据库中,短信猫直接读取数据库,将短信发送出去。



手机叮叮一响,短信收到。完工。

评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值