Exemplo n.º 1
0
        /// <summary>
        /// 发送超长短信
        /// </summary>
        /// <param name="center">短信中心号码</param>
        /// <param name="phone">接收者号码</param>
        /// <param name="count">短信分条总数</param>
        /// <param name="index">短信分条索引</param>
        /// <param name="content">已编码短信内容</param>
        /// <param name="length">当前分条的长度</param>
        /// <returns>当前分条短信的内容</returns>
        public static string encodingSMS(string center, string phone, int count, int index, string content, out string length)
        {
            content = encodingContent(content);
            string message  = string.Empty;
            int    msgcount = SMSPDUCoding.maxEncodeCharCount() * 2;//固定

            if (index == count)
            {
                message = content.Substring((index - 1) * msgcount); //共可发送134个编码
            }
            else
            {
                message = content.Substring((index - 1) * msgcount, msgcount);
            }

            //加密消息中心号码
            //center = DecodingCenter(center);

            //string result = string.Format("005100{0}{1}0008A7{2:X2}05000304{3:D2}{4:D2}{5}",
            //                            center,
            //                            DecodingPhone(phone),
            //                            (message.Length + 12) / 2,
            //                            count,
            //                            i,
            //                            message);

            //length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度

            /**/
            ////string ph = phone;
            ////phone = DecodingPhone(phone).Substring(2);

            ////string result = string.Format("005100{0}{1}0008A7{2:X2}05000304{3:D2}{4:D2}{5}",
            ////                              string.Format("{0:X2}A1", ph.Length),
            ////                            phone,
            ////                            (message.Length + 12) / 2,
            ////                            count,
            ////                            i,
            ////                            message);

            string result = String.Format("{0}51000D91{1}000800{2:X2}05000304{4:D2}{5:D2}{3}",
                                          center,
                                          decodingPhone(phone),
                                          message.Length / 2 + 6,
                                          message,
                                          count,
                                          index
                                          );

            length = String.Format("{0:D2}", result.Length / 2 - center.Length / 2);//获取短信内容加上手机号码长度

            return(result);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 发送短信, 自动分段
 /// </summary>
 /// <param name="service_center">短信中心号码</param>
 /// <param name="text">短信内容</param>
 /// <param name="target_phone">接收手机号码</param>
 public string send(string service_center, string text, string target_phone)
 {
     try
     {
         string response = "";
         if (ss_port.IsOpen)
         {
             ss_port.Close();
         }
         ss_port.Open();
         string length = "";
         if (text.Length <= SMSPDUCoding.MAX_CHAR_COUNT)
         {
             #region Short SMS Send
             string smsTemp = SMSPDUCoding.encodingSMS("00", target_phone, text, out length);
             ss_port.WriteLine("atz");                                  //短信猫初始化命令
             ss_port.WriteLine("at + cmgf=0");                          //以PDU编码格式发送短信
             ss_port.WriteLine(String.Format("at + cmgs={0}", length)); //设置通信内容长度
             ss_port.Write(smsTemp);                                    //写入PDU编码的通信内容
             ss_port.WriteLine("\x01a");                                //Ctrl + Z 发送短信
             while (true)
             {
                 try
                 {
                     string res = readComm();
                     response += res;
                     if (res == "")
                     {
                         break;
                     }
                 }
                 catch { break; }
             }
             if (response.IndexOf("ERROR") >= 0)
             {
                 ss_port.Close();
                 return("短信发送失败,信息:" + response);
             }
             #endregion
         }
         else
         {
             #region Long SMS Send
             ArrayList list  = new ArrayList();
             int       start = 0;
             while (true)
             {
                 string msg = "";
                 if (start + SMSPDUCoding.MAX_CHAR_COUNT < text.Length)
                 {
                     msg = text.Substring(start, SMSPDUCoding.MAX_CHAR_COUNT);
                     list.Add(msg);
                     start += SMSPDUCoding.MAX_CHAR_COUNT;
                 }
                 else
                 {
                     msg = text.Substring(start);
                     list.Add(msg);
                     break;
                 }
             }
             for (int i = 0; i < list.Count; i++)
             {
                 string smsTemp = SMSPDUCoding.encodingSMS(service_center, target_phone, list.Count, i + 1, text, out length);
                 ss_port.WriteLine("atz");                                  //短信猫初始化命令
                 ss_port.WriteLine("at + cmgf=0");                          //以PDU编码格式发送短信
                 ss_port.WriteLine(String.Format("at + cmgs={0}", length)); //设置通信内容长度
                 ss_port.Write(smsTemp);                                    //写入PDU编码的通信内容
                 ss_port.WriteLine("\x01a");                                //Ctrl + Z 发送短信
                 while (true)
                 {
                     try
                     {
                         string res = readComm();
                         response += res;
                         if (res == "")
                         {
                             break;
                         }
                     }
                     catch { break; }
                 }
                 if (response.IndexOf("ERROR") >= 0)
                 {
                     ss_port.Close();
                     return("短信发送失败,信息:" + response);
                 }
             }
             #endregion
         }
         if (ss_port.IsOpen)
         {
             ss_port.Close();
         }
         return("");
     }
     catch (Exception ex) { if (ss_port.IsOpen)
                            {
                                ss_port.Close();
                            }
                            return(ex.Message); }
 }
Exemplo n.º 3
0
 /// <summary>
 /// 发送短信
 /// </summary>
 /// <param name="service_center">短信中心号码</param>
 /// <param name="text">短信内容</param>
 /// <param name="target_phone">接收手机号码</param>
 /// <param name="count">短信分条总量</param>
 /// <param name="index">当前第几条</param>
 /// <returns>成功返回空字符串,失败返回失败信息</returns>
 public string send(string service_center, string text, string target_phone, int count, int index)
 {
     try
     {
         string response = "";
         if (ss_port.IsOpen)
         {
             ss_port.Close();
         }
         ss_port.Open();
         string length = "";
         if (count == 1)
         {
             #region Short SMS Send
             string smsTemp = SMSPDUCoding.encodingSMS("00", target_phone, text, out length);
             ss_port.WriteLine("atz");                                  //短信猫初始化命令
             ss_port.WriteLine("at + cmgf=0");                          //以PDU编码格式发送短信
             ss_port.WriteLine(String.Format("at + cmgs={0}", length)); //设置通信内容长度
             ss_port.Write(smsTemp);                                    //写入PDU编码的通信内容
             ss_port.WriteLine("\x01a");                                //Ctrl + Z 发送短信
             while (true)
             {
                 try
                 {
                     string res = readComm();
                     response += res;
                     if (res == "")
                     {
                         break;
                     }
                 }
                 catch { break; }
             }
             if (response.IndexOf("ERROR") >= 0)
             {
                 ss_port.Close();
                 return("短信发送失败,信息:" + response);
             }
             #endregion
         }
         else
         {
             #region Long SMS Send
             string smsTemp = SMSPDUCoding.encodingSMS(service_center, target_phone, count, index, text, out length);
             ss_port.WriteLine("atz");                                  //短信猫初始化命令
             ss_port.WriteLine("at + cmgf=0");                          //以PDU编码格式发送短信
             ss_port.WriteLine(String.Format("at + cmgs={0}", length)); //设置通信内容长度
             ss_port.Write(smsTemp);                                    //写入PDU编码的通信内容
             ss_port.WriteLine("\x01a");                                //Ctrl + Z 发送短信
             while (true)
             {
                 try
                 {
                     string res = readComm();
                     response += res;
                     if (res == "")
                     {
                         break;
                     }
                 }
                 catch { break; }
             }
             if (response.IndexOf("ERROR") >= 0)
             {
                 ss_port.Close();
                 return("短信发送失败,信息:" + response);
             }
             #endregion
         }
         if (ss_port.IsOpen)
         {
             ss_port.Close();
         }
         return("");
     }
     catch (Exception ex) { if (ss_port.IsOpen)
                            {
                                ss_port.Close();
                            }
                            return(ex.Message); }
 }