使用EasySMPP API发送Concatinated SMS消息(如何添加UDH信息)

时间:2018-03-22 11:10:44

标签: sms smpp pdu

我正在使用名为EasySMPP的c#API,它在发送单个短信方面相当不错,它也很好发送大型短信,但收件人分别收到邮件,这没有意义,我正在寻找的是如何修改PDU,以便我可以附加UDH信息。

如何添加UHD信息,这里是API的SubmitSM方法,

public int SubmitSM
(
byte sourceAddressTon, 
byte sourceAddressNpi, 
string sourceAddress,
byte destinationAddressTon,
byte destinationAddressNpi, 
destinationAddress, 
byte esmClass, 
byte protocolId,
byte priorityFlag, 
DateTime sheduleDeliveryTime, 
DateTime validityPeriod, 
byte registeredDelivery,
byte replaceIfPresentFlag,
byte dataCoding,
byte smDefaultMsgId,
byte[] message)

 {
 try
 {

                byte[] _destination_addr;
                byte[] _source_addr;
                byte[] _SUBMIT_SM_PDU;
                byte[] _shedule_delivery_time;
                byte[] _validity_period;
                int _sequence_number;
                int pos;
                byte _sm_length;

                _SUBMIT_SM_PDU = new byte[KernelParameters.MaxPduSize];

                ////////////////////////////////////////////////////////////////////////////////////////////////
                /// Start filling PDU                       

                Tools.CopyIntToArray(0x00000004, _SUBMIT_SM_PDU, 4);
                _sequence_number = smscArray.currentSMSC.SequenceNumber;
                Tools.CopyIntToArray(_sequence_number, _SUBMIT_SM_PDU, 12);


                pos = 16;
                _SUBMIT_SM_PDU[pos] = 0x00; //service_type
                pos += 1;
                _SUBMIT_SM_PDU[pos] = sourceAddressTon;
                pos += 1;
                _SUBMIT_SM_PDU[pos] = sourceAddressNpi;
                pos += 1;
                _source_addr = Tools.ConvertStringToByteArray(Tools.GetString(sourceAddress, 20, ""));
                Array.Copy(_source_addr, 0, _SUBMIT_SM_PDU, pos, _source_addr.Length);
                pos += _source_addr.Length;
                _SUBMIT_SM_PDU[pos] = 0x00;
                pos += 1;
                _SUBMIT_SM_PDU[pos] = destinationAddressTon;
                pos += 1;
                _SUBMIT_SM_PDU[pos] = destinationAddressNpi;
                pos += 1;
                _destination_addr = Tools.ConvertStringToByteArray(Tools.GetString(destinationAddress, 20, ""));
                Array.Copy(_destination_addr, 0, _SUBMIT_SM_PDU, pos, _destination_addr.Length);
                pos += _destination_addr.Length;
                _SUBMIT_SM_PDU[pos] = 0x00;
                pos += 1;
                _SUBMIT_SM_PDU[pos] = esmClass;
                pos += 1;
                _SUBMIT_SM_PDU[pos] = protocolId;
                pos += 1;
                _SUBMIT_SM_PDU[pos] = priorityFlag;
                pos += 1;
                _shedule_delivery_time = Tools.ConvertStringToByteArray(Tools.GetDateString(sheduleDeliveryTime));
                Array.Copy(_shedule_delivery_time, 0, _SUBMIT_SM_PDU, pos, _shedule_delivery_time.Length);
                pos += _shedule_delivery_time.Length;
                _SUBMIT_SM_PDU[pos] = 0x00;
                pos += 1;
                _validity_period = Tools.ConvertStringToByteArray(Tools.GetDateString(validityPeriod));
                Array.Copy(_validity_period, 0, _SUBMIT_SM_PDU, pos, _validity_period.Length);
                pos += _validity_period.Length;
                _SUBMIT_SM_PDU[pos] = 0x00;
                pos += 1;
                _SUBMIT_SM_PDU[pos] = registeredDelivery;
                pos += 1;
                _SUBMIT_SM_PDU[pos] = replaceIfPresentFlag;
                pos += 1;
                _SUBMIT_SM_PDU[pos] = dataCoding;
                pos += 1;
                _SUBMIT_SM_PDU[pos] = smDefaultMsgId;
                pos += 1;

                _sm_length = message.Length > 254 ? (byte)254 : (byte)message.Length;

                _SUBMIT_SM_PDU[pos] = _sm_length;
                pos += 1;
                Array.Copy(message, 0, _SUBMIT_SM_PDU, pos, _sm_length);
                pos += _sm_length;

                Tools.CopyIntToArray(pos, _SUBMIT_SM_PDU, 0);

                Send(_SUBMIT_SM_PDU, pos);

                undeliveredMessages++;
                return _sequence_number;
            }
            catch (Exception ex)
            {
                logMessage(LogLevels.LogExceptions, "SubmitSM | " + ex.ToString());
            }
            return -1;

        }  

非常感谢!!!!

2 个答案:

答案 0 :(得分:0)

正如我所看到的,EasySMPP可以选择拆分长文本。

public int SendSms(String from,String to,bool splitLongText,String text,byte askDeliveryReceipt,byte esmClass,byte dataCoding)

否则,smpp协议也可以手动执行。 在SAR参数中:

SAR_MSG_REF_NUM - reference, unique to all sms parts
SAR_SEGMENT_SEQNUM - sequence number (1,2,3...)
SAR_TOTAL_SEGMENTS - number of all sequences

或者您可以封装在消息的开头 将UDHI设置为64

或者您可以在MESSAGE_PAYLOAD TLV中设置消息,SMSC将为您完成。

希望有所帮助

答案 1 :(得分:0)

以下是添加UDH的方法。

public int SendSms(String from, String to,
bool splitLongText, String text,       
byte askDeliveryReceipt, 
 byte esmClass, byte dataCoding)
        {
 int messageId = -1;
            byte sourceAddressTon;
            byte sourceAddressNpi;
            string sourceAddress;
            byte destinationAddressTon;
            byte destinationAddressNpi;
            string destinationAddress;
            byte registeredDelivery;
            byte maxLength;

            sourceAddress = Tools.GetString(from, 20, "");
            sourceAddressTon = getAddrTon(sourceAddress);
            sourceAddressNpi = getAddrNpi(sourceAddress);

            destinationAddress = Tools.GetString(to, 20, "");
            destinationAddressTon = getAddrTon(destinationAddress);
            destinationAddressNpi = getAddrNpi(destinationAddress);

            registeredDelivery = askDeliveryReceipt;

            if (dataCoding == 8)
            {
               // text = Tools.Endian2UTF(text);
                maxLength = 70;
            }
            else
                maxLength = 160;

            if ((text.Length <= maxLength) || (splitLongText))
            {
                byte protocolId;
                byte priorityFlag;
                DateTime sheduleDeliveryTime;
                DateTime validityPeriod;
                byte replaceIfPresentFlag;
                byte smDefaultMsgId;
              //  byte[] message = new byte[146];
                byte[] udh = new byte[6];
                string smsText = text;
                byte[] message;
                protocolId = 0;
                priorityFlag = PriorityFlags.VeryUrgent;
                sheduleDeliveryTime = DateTime.MinValue;
                validityPeriod = DateTime.MinValue;
                replaceIfPresentFlag =   
               ReplaceIfPresentFlags.DoNotReplace;
                 smDefaultMsgId = 0;
                if (dataCoding == 8)
                {
                    message = new byte[70];
                }
                else
                {
                     message = new byte[146];
                }

                string[] lists = dataCoding == 8 ?                             
                 split_message_unicode(text) : 
                 split_message_asci(text);


                int count = 1;
                foreach (string s in lists)
                {
                    Array.Clear(message, 0, message.Length);
                  //  while (smsText.Length > 0)
                   // {
                        int pos = 0;
                       // byte desc = Convert.ToByte('c');

                        byte headerLen = Convert.ToByte(05);
                        byte concat = Convert.ToByte(00);
                        byte refNo = Convert.ToByte(03);
                        byte sequenceNo = Convert.ToByte(03);
                        byte NoOfMessages = Convert.ToByte(lists.Length);
                        byte partNo = Convert.ToByte(count);
                        count++;


                        udh[pos] = headerLen;
                        pos++;
                        udh[pos] = concat;
                        pos++;
                        udh[pos] = refNo;
                        pos++;
                        udh[pos] = sequenceNo;
                        pos++;
                        udh[pos] = NoOfMessages;
                        pos++;
                        udh[pos] = partNo;
                        pos++;

                        Array.Copy(udh, 0, message, 0, pos);

                        if (dataCoding == 8)
                        {
                            int len = Tools.GetEthiopic(s).Length;
                            Array.Copy(Tools.GetEthiopic(s), 0, message,  
       pos, len);
                            //message = 
          Tools.GetEthiopic(smsText.Substring(0, smsText.Length > 
        maxLength ? maxLength : smsText.Length));
                            //message = 
       Encoding.UTF8.GetBytes(smsText.Substring(0, smsText.Length > 
         maxLength ? maxLength : smsText.Length));
                        }
                        else{
                           // message = Encoding.ASCII.GetBytes(s);
                            Array.Copy(Encoding.ASCII.GetBytes(s), 0, 
           message, pos, Encoding.ASCII.GetBytes(s).Length);
                        }
                        smsText = smsText.Remove(0, smsText.Length > 
             maxLength ? maxLength : smsText.Length);


                        messageId = SubmitSM(sourceAddressTon, 
       sourceAddressNpi, sourceAddress,
                                            destinationAddressTon, 
         destinationAddressNpi, destinationAddress,
                                            esmClass, protocolId, 
        priorityFlag,
                                            sheduleDeliveryTime, 
       validityPeriod, registeredDelivery, replaceIfPresentFlag,
                                            dataCoding, smDefaultMsgId, 
             message);


                  //  }
                }

            }
            else
            {
                byte[] data;

                if (dataCoding == 8)
                    data = Tools.GetEthiopic(text);
                    //data = Encoding.UTF8.GetBytes(text);
                else
                    data = Encoding.ASCII.GetBytes(text);
                messageId = DataSM(sourceAddressTon, sourceAddressNpi, 
         sourceAddress,
                                    destinationAddressTon, 
         destinationAddressNpi, destinationAddress,
                                    esmClass, registeredDelivery, 
           dataCoding, data);
            }
            return messageId;
             }

将ems_class传递为0x40,您需要的只是将您的邮件拆分为部分,这里是拆分方法,

       private string[] split_message_asci(string message)
        {
            int message_len = message.Length;
            decimal val=(message.Length / 140m);
            decimal parts = Math.Ceiling(val);
            string[] strs = new string[(int)parts];
            string str = string.Empty;
            int interval=140;
            int i =0;
            int Count= 0;
            while (i < message_len)
            {
                if (Count < (int)parts-1)
                {
                    str = message.Substring(i, interval);
                    strs[Count] = str;
                    i += interval ;
                }
                else
                {
                    str = message.Substring(i, message_len-i);
                    strs[Count] = str;
                    i += interval + 1;
                }

                Count++;
            }

            return strs;
        }