使用jamaa超过160个字符的SMPP消息长度错误

时间:2013-11-11 09:37:32

标签: asp.net smpp

我正在使用jamaa-smpp发送短信。但我无法使用api发送超过160个字符。我使用以下链接http://jamaasmpp.codeplex.com/

代码如下

 SmppConnectionProperties properties = _client.Properties;
  properties.SystemID = "test";
  properties.Password = "test1";
  properties.Port = 101; //IP port to use
  properties.Host = "..."; //SMSC host name or IP Address
  ....

是否可以使用该API发送超过160个字符。

1 个答案:

答案 0 :(得分:3)

我在网站discussion找到了解决方案。我在这里发帖,以便人们可以在这里找到解决方案。

我替换了TextMessage.csJamaaTech.Smpp.Net.Client)中的现有函数。
函数名称为IEnumerable<SendSmPDU> GetPDUs(DataCoding defaultEncoding)

 //protected override IEnumerable<SendSmPDU> GetPDUs(DataCoding defaultEncoding)
    //{
    //    //This smpp implementation does not support sending concatenated messages,
    //    //however, concatenated messages are supported on the receiving side.
    //    int maxLength = GetMaxMessageLength(defaultEncoding, false);
    //    byte[] bytes = SMPPEncodingUtil.GetBytesFromString(vText, defaultEncoding);
    //    //Check message size
    //    if(bytes.Length > maxLength)
    //    {
    //        throw new InvalidOperationException(string.Format(
    //            "Encoding '{0}' does not support messages of length greater than '{1}' charactors",
    //            defaultEncoding, maxLength));
    //    }
    //    SubmitSm sm = new SubmitSm();
    //    sm.SetMessageBytes(bytes);
    //    sm.SourceAddress.Address = vSourceAddress;
    //    sm.DestinationAddress.Address = vDestinatinoAddress;
    //    sm.DataCoding = defaultEncoding;
    //    if (vRegisterDeliveryNotification) { sm.RegisteredDelivery = RegisteredDelivery.DeliveryReceipt; }
    //    yield return sm;
    //}
    protected override IEnumerable<SendSmPDU> GetPDUs(DataCoding defaultEncoding)
    {
        SubmitSm sm = new SubmitSm();
        sm.SourceAddress.Address = vSourceAddress;
        sm.DestinationAddress.Address = vDestinatinoAddress;
        sm.DataCoding = defaultEncoding;
        if (vRegisterDeliveryNotification)
            sm.RegisteredDelivery = RegisteredDelivery.DeliveryReceipt;
        int maxLength = GetMaxMessageLength(defaultEncoding, false);
        byte[] bytes = SMPPEncodingUtil.GetBytesFromString(vText, defaultEncoding);
        if (bytes.Length > maxLength)
        {
            var SegID = new Random().Next(1000, 9999); 
            var messages = Split(vText, GetMaxMessageLength(defaultEncoding, true));
            var totalSegments = messages.Count;
            var udh = new Udh(SegID, totalSegments, 0);
            for (int i = 0; i < totalSegments; i++)
            {
                udh.MessageSequence = i + 1;
                sm.Header.SequenceNumber = PDUHeader.GetNextSequenceNumber(); 
                sm.SetMessageText(messages[i], defaultEncoding, udh);
                yield return sm;
            }
        }
        else
        {
            sm.SetMessageBytes(bytes);
            yield return sm;
        }
    }

    private static List<String> Split(string message, int maxPartLength)
    {
        var result = new List<String>();

        for (int i = 0; i < message.Length; i += maxPartLength)
        {
            var chunkSize = i + maxPartLength < message.Length ? maxPartLength : message.Length - i;
            var chunk = new char[chunkSize];
            message.CopyTo(i, chunk, 0, chunkSize);
            result.Add(new string(chunk));
        }
        return result;
    }