예제 #1
0
        /// <summary>
        /// 发送短信 (重载)
        /// </summary>
        /// <param name="phone">手机号码</param>
        /// <param name="msg">短信内容</param>
        /// <param name="msgType">短信类型</param>
        public void SendMsg(string phone, string msg, MsgType msgType)
        {
            if (msgType == MsgType.AUSC2)
            {
                SendMsg(phone, msg);
            }
            else
            {
                PDUEncoding pe = new PDUEncoding();

                //短信中心号码 服务中心地址
                pe.ServiceCenterAddress = msgCenter;


                string temp = pe.PDU7BitEncoder(phone, msg);


                //计算长度
                int len = (temp.Length - Convert.ToInt32(temp.Substring(0, 2), 16) * 2 - 2) / 2;

                try
                {
                    //26 Ctrl+Z ascii码
                    temp = SendAT("AT+CMGS=" + len.ToString() + "\r" + temp + (char)(26));
                }
                catch (Exception)
                {
                    throw new Exception("短信发送失败");
                }

                if (temp.Substring(temp.Length - 4, 3).Trim() == "OK")
                {
                    return;
                }

                throw new Exception("短信发送失败");
            }
        }
예제 #2
0
        /// <summary>
        /// 取得已发信息列表
        /// </summary>
        /// <returns>已发信息列表(中心号码,手机号码,发送时间,短信内容)</returns>
        public List <string> GetIsSendMsg()
        {
            // 预期结果.
            List <string> result = new List <string>();

            string[] temp = null;
            string   tt   = string.Empty;

            // 读取已发信息
            tt = this.SendAT("AT+CMGL=3");


            if (tt.EndsWith("OK\r"))
            {
                temp = tt.Split('\r');
            }

            if (tt.EndsWith("ERROR\r"))
            {
                return(result);
            }


            PDUEncoding pe = new PDUEncoding();

            // 计数
            int i = 0;

            foreach (string str in temp)
            {
                if (str != null && str.Length != 0 && str.Substring(0, 2).Trim() != "+C" && str.Substring(0, 2) != "OK" && str.Substring(0, 2) != "AT")
                {
                    result.Add(pe.PDUDecoder(str));
                    i++;
                }
            }
            return(result);
        }
예제 #3
0
        /// <summary>
        /// 取得未读信息列表
        /// </summary>
        /// <returns>未读信息列表(中心号码,手机号码,发送时间,短信内容)</returns>
        public List<string> GetUnReadMsg()
        {
            // 预期结果.
            List<string> result = new List<string>();

            string[] temp = null;
            string tt = string.Empty;

            // 读取未读信息
            tt = this.SendAT("AT+CMGL=0");

            if (tt.EndsWith("OK\r"))
            {
                temp = tt.Split('\r');
            }

            if (tt.EndsWith("ERROR\r"))
            {
                return result;
            }

            
            PDUEncoding pe = new PDUEncoding();

            // 计数
            int i = 0;
            foreach (string str in temp)
            {
                if (str != null && str.Length != 0 && str.Substring(0, 2).Trim() != "+C" && str.Substring(0, 2) != "OK" && str.Substring(0, 2) != "AT")
                {
                    result.Add(pe.PDUDecoder(str));
                    i++;
                }
            }
            return result;
        }
예제 #4
0
        /// <summary>
        /// 发送短信 (重载)
        /// </summary>
        /// <param name="phone">手机号码</param>
        /// <param name="msg">短信内容</param>
        /// <param name="msgType">短信类型</param>
        public void SendMsg(string phone, string msg, MsgType msgType)
        {
            if (msgType == MsgType.AUSC2)
            {
                SendMsg(phone, msg);
            }
            else
            {

                PDUEncoding pe = new PDUEncoding();

                //短信中心号码 服务中心地址
                pe.ServiceCenterAddress = msgCenter;                    


                string temp = pe.PDU7BitEncoder(phone, msg);


                //计算长度
                int len = (temp.Length - Convert.ToInt32(temp.Substring(0, 2), 16) * 2 - 2) / 2;  

                try
                {
                    //26 Ctrl+Z ascii码
                    temp = SendAT("AT+CMGS=" + len.ToString() + "\r" + temp + (char)(26));  
                }
                catch (Exception)
                {
                    throw new Exception("短信发送失败");
                }

                if (temp.Substring(temp.Length - 4, 3).Trim() == "OK")
                {
                    return;
                }

                throw new Exception("短信发送失败");
            }
        }