C# 如何发送超过70个字符的短信
如果要发送的短消息少于70个字符,我的代码工作正常 我想发送消息,每条消息包含70到200个字符C# 如何发送超过70个字符的短信,c#,unicode,pdu,C#,Unicode,Pdu,如果要发送的短消息少于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()
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);
}
}
}
}如果您愿意,您可以看到有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
}
您被限制为67个字符。请参阅:@jdweng OP没有使用Twilio,他们直接使用GSM调制解调器。在这种情况下,它取决于调制解调器提供的功能。它仍然是SMS的标准。承运人不重要。我做的完全正确,但我收到的信息是一部分,例如,一条信息中有70个字母,另一条信息中有70个字母,我希望所有信息都包含在一条信息中。如果你明白我的意思,请编辑我的代码。如果不明确地调整源代码并自己重新编译它,这是不可能的(你可以使用我文章前面提供的链接)。只需将该常量值从70个字符调整为Int.MaxValue或类似值。我怀疑这是因为一个特定的原因而实现的,这可能只是这些消息发送方式的一个限制。我已经找到了解决方案,现在它工作得很好。
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);
}