【问题标题】:How to send SMS with more then 70 characters如何发送超过 70 个字符的短信
【发布时间】:2020-04-29 02:04:30
【问题描述】:

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

}

【问题讨论】:

  • 您被限制为 67 个字符。请参阅:twilio.com/docs/glossary/what-sms-character-limit
  • @jdweng OP 没有使用 Twilio,他们直接使用 GSM 调制解调器。在这种情况下,它取决于调制解调器提供的功能。
  • 它仍然是 SMS 的标准。与承运人无关。

标签: c# unicode pdu


【解决方案1】:

如果您take a look at the source here for the SmsPdu class here,您会看到明确限制为 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);
}

【讨论】:

  • 我确实在做但是我收到一条消息,例如,一条消息中有 70 个字母,另一条消息中有 70 个字母,我想要一条消息中的所有字母。如果你明白我的意思,请编辑我的代码。
  • 如果不自己显式调整源代码并自己重新编译,这是不可能的(您可以使用我帖子前面提供的链接)。只需将该常量值从 70 个字符调整为 Int.MaxValue 或类似的值。我怀疑这是出于特定原因而实施的,这可能只是对这些消息的发送方式的限制。
  • 我已经找到了解决方案,现在它运行良好。 stackoverflow.com/questions/51136293/…
猜你喜欢
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
相关资源
最近更新 更多