Как отправить СМС длиной более 70 символов - PullRequest
0 голосов
/ 11 января 2020

Мой код работает отлично, если отправляемые SMS-сообщения содержат менее 70 символов.

Я хочу отправлять сообщения длиной от 70 до 200 символов в одном сообщении.

using GsmComm.GsmCommunication;
using GsmComm.PduConverter;
using GsmComm.Server;
using GsmComm.PduConverter.SmartMessaging;

namespace SMSSender
{
public partial class Form1 : Form
{

public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {

        try
        {
            string msg = " کو  ہم نے";
            GsmCommMain comm = new GsmCommMain(4, 19200, 500);
            comm.Open();           
            SmsSubmitPdu pdu;
            pdu = new SmsSubmitPdu(msg, "03319310077", DataCodingScheme.NoClass_16Bit);              
            comm.SendMessage(pdu);
        }
            catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

}

1 Ответ

0 голосов
/ 12 января 2020

Если вы посмотрите на источник здесь для SmsPdu класса здесь , вы увидите, что существует явное ограничение в 70 символов Юникода, которое объяснит проблему, с которой вы столкнулись:

public abstract class SmsPdu : ITimestamp
{
        // Omitted for brevity 

        /// <summary>
        /// Gets the maximum Unicode message text length in characters.
        /// </summary>
        public const int MaxUnicodeTextLength = 70;

        // Omitted for brevity
}

Возможный обходной путь

Возможный обходной путь может включать разбиение одного сообщения на несколько пакетов длиной менее 70 символов и отправку нескольких в один адресат:

public static IEnumerable<string> BatchMessage(string message, int batchSize = 70)
{
        if (string.IsNullOrEmpty(message))
        {
            // Message is null or empty, handle accordingly
        }

        if (batchSize < message.Length)
        {
            // Batch is smaller than message, handle accordingly    
        }

        for (var i = 0; i < message.Length; i += batchSize)
        {
            yield return message.Substring(i, Math.Min(batchSize, message.Length - i));
        }
}

И затем просто позвоните до отправки сообщения и отправьте пакеты по отдельности:

// Open your connection
GsmCommMain comm = new GsmCommMain(4, 19200, 500);
comm.Open();  

// Store your destination
var destination = "03319310077";

// Batch your message into one or more
var messages = BatchMessage(" کو  ہم نے");
foreach (var message in messages)
{
    // Send each one
    var sms = new SmsSubmitPdu(message, destination, DataCodingScheme.NoClass_16Bit);
    comm.SendMessage(sms);
}
...