Exemplo n.º 1
0
        private bool SendPageSMS(string PhoneNum, string SmsMsg)
        {
            SmsBuilder SmsBdr = new SmsBuilder(PhoneNum);
            // 未处理分页时,转到处理分页,并发送短信的函数
            if (SmsBdr.NeedPage(SmsMsg) == true)
            {
                // return this.SendSMS(PhoneNum, SmsMsg, SmsSource);
            }
            string[] Params = "203.81.21.56|3000|10060|8|message1,message2,message3,message4,message5".Split('|');
            UdpClient uc = new UdpClient();
            IPAddress ipa = IPAddress.Parse(Params[0]);
            int port = Convert.ToInt16(Params[1]);
            IPEndPoint ipep = new IPEndPoint(ipa, port);

            // 生成短信来源
            string SmsSrc = "web";
            SmpApi sa = new SmpApi();
            sa.setSmp021(PhoneNum);
            sa.setSmp023(Params[2]);
            sa.setSmp032(SmsMsg);
            sa.setSmp040(Params[3]);
            sa.setSmp085(SmsSrc);
            sa.setSmp084("101");

            byte[] Buf = Encoding.Default.GetBytes(sa.packSmp());

            // 循环重试发送短信

            for (int i = 0; i < SmsBdr.TrySendCount; i++)
            {
                try
                {
                    // 调用发送短信接口,将短信发送出去
                    uc.Send(Buf, Buf.Length, ipep);
                    break;
                }
                catch (Exception ex)
                {
                    if (i < SmsBdr.TrySendCount - 1)
                    {
                        // 重试发送前,等待一个时间间隔
                        System.Threading.Thread.Sleep(SmsBdr.RetryInterval);
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            return true;
        }
Exemplo n.º 2
0
 public bool SendSMS( string PhoneNum, string SmsMsg)
 {
     SmsBuilder SmsBdr = new SmsBuilder ( PhoneNum );
     // 当手机号无效,取消发送
     if ( SmsBdr.IsValid () == false )
     {
         return false;
     }
     else
     {
         // 短信不用分页时,直接发送单条短信
         if ( SmsBdr.NeedPage ( SmsMsg ) == false )
         {
             // 生成单页短信
             string SingleMsg = SmsBdr.SingleMsg ( SmsMsg );
             // 发送单页短信
             return this.SendPageSMS(PhoneNum, SingleMsg);
         }
         else// 短信需要分页时,依次发送多条已分页的短信
         {
             // 短信分页页数
             int SmsPageCount = SmsBdr.PageCount ( SmsMsg );
             bool RtnVal = true;
             for ( int Index = 0; Index < SmsPageCount; Index ++ )
             {
                 // 生成当前页短信
                 string SmsPageMsg = SmsBdr.PageMsg ( SmsMsg, Index );
                 // 发送当前页短信
                 RtnVal &= this.SendPageSMS(PhoneNum, SmsPageMsg);
                 // 当前页不是最后一页
                 if ( Index < SmsPageCount )
                 {
                     // 发送两页短信之间,等待一个时间间隔
                     System.Threading.Thread.Sleep ( SmsBdr.PageInterval );
                 }
             }
             return RtnVal;
         }
     }
 }