0

如果要发送的 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);
        }
    }
}

}

4

1 回答 1

0

如果您在此处查看SmsPdu该类的源代码,您可以看到明确限制为 70 个 Unicode 字符,这可以解释您遇到的问题:

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);
}
于 2020-01-12T02:40:12.930 回答