private async Task SubmitBatchMessages() { var sourceAddress = new SmeAddress(tbSrcAdr.Text, (AddressTON)byte.Parse(tbSrcAdrTON.Text), (AddressNPI)byte.Parse(tbSrcAdrNPI.Text)); var destinationAddress = new SmeAddress(tbDestAdr.Text, (AddressTON)byte.Parse(tbDestAdrTON.Text), (AddressNPI)byte.Parse(tbDestAdrNPI.Text)); string messageText = tbMessageText.Text; SubmitMode mode = GetSubmitMode(); DataCodings coding = GetDataCoding(); int count = int.Parse(tbRepeatTimes.Text); _log.Info("Submit message batch. Count: {0}. Text: {1}", count, messageText); // bulk sms test List <SubmitSm> batch = new List <SubmitSm>(); for (int i = 0; i < count; i++) { ISubmitSmBuilder builder = SMS.ForSubmit() .Text(messageText) .From(sourceAddress) .To(destinationAddress) .Coding(coding); switch (mode) { case SubmitMode.Payload: builder.MessageInPayload(); break; case SubmitMode.ShortMessageWithSAR: builder.ConcatenationInSAR(); break; } batch.AddRange(builder.Create(_client)); } try { Stopwatch watch = Stopwatch.StartNew(); var resp = (await _client.Submit(batch)).ToArray(); watch.Stop(); if (resp.All(x => x.Header.Status == CommandStatus.ESME_ROK)) { _log.Info("Batch sending completed. Submitted: {0}, Elapsed: {1} ms, Performance: {2} m/s", batch.Count, watch.ElapsedMilliseconds, batch.Count * 1000f / watch.ElapsedMilliseconds); } else { var wrongStatuses = resp.Where(x => x.Header.Status != CommandStatus.ESME_ROK) .Select(x => x.Header.Status).Distinct(); _log.Warn("Submit failed. Wrong Status: {0}", string.Join(", ", wrongStatuses)); } } catch (Exception ex) { _log.Error("Submit failed. Error: {0}", ex.Message); } }
private async Task SubmitMultiMessage(string[] dstAddresses) { DataCodings coding = GetDataCoding(); byte srcTon = byte.Parse(tbSrcAdrTON.Text); byte srcNpi = byte.Parse(tbSrcAdrNPI.Text); string srcAdr = tbSrcAdr.Text; byte dstTon = byte.Parse(tbDestAdrTON.Text); byte dstNpi = byte.Parse(tbDestAdrNPI.Text); ISubmitMultiBuilder builder = SMS.ForSubmitMulti() .From(srcAdr, (AddressTON)srcTon, (AddressNPI)srcNpi) .Coding(coding) .Text(tbMessageText.Text) //Request delivery receipt .DeliveryReceipt(); foreach (var dstAddress in dstAddresses) { if (dstAddress == null || dstAddress.Trim().Length == 0) { continue; } builder.To(dstAddress.Trim(), (AddressTON)dstTon, (AddressNPI)dstNpi); } _log.Info("Submit message to several addresses: {0}. Text: {1}", string.Join(", ", dstAddresses), tbMessageText.Text); SubmitMode mode = GetSubmitMode(); switch (mode) { case SubmitMode.Payload: builder.MessageInPayload(); break; case SubmitMode.ShortMessageWithSAR: builder.ConcatenationInSAR(); break; } try { IList <SubmitMultiResp> resp = await _client.Submit(builder); if (resp.All(x => x.Header.Status == CommandStatus.ESME_ROK)) { _log.Info("Submit succeeded. MessageIds: {0}", string.Join(",", resp.Select(x => x.MessageId))); } else { _log.Warn("Submit failed. Status: {0}", string.Join(",", resp.Select(x => x.Header.Status.ToString()))); } } catch (Exception ex) { _log.Error("Submit failed. Error: {0}", ex.Message); } }
private async Task SubmitSingleMessage() { DataCodings coding = GetDataCoding(); var sourceAddress = new SmeAddress(tbSrcAdr.Text, (AddressTON)byte.Parse(tbSrcAdrTON.Text), (AddressNPI)byte.Parse(tbSrcAdrNPI.Text)); var destinationAddress = new SmeAddress(tbDestAdr.Text, (AddressTON)byte.Parse(tbDestAdrTON.Text), (AddressNPI)byte.Parse(tbDestAdrNPI.Text)); _log.Info("Submit message To: {0}. Text: {1}", tbDestAdr.Text, tbMessageText.Text); ISubmitSmBuilder builder = SMS.ForSubmit() .From(sourceAddress) .To(destinationAddress) .Coding(coding) .Text(tbMessageText.Text) //Or you can set data //.Data(HexStringToByteArray("024A3A6949A59194813D988151A004004D215D2690568698A22820C49A4106288A126A8A22C2A820C22820C2A82342AC30820C4984106288A12628A22C2A420800")) //Apply italic style for all text (mobile device should support basic EMS) //.Set(delegate(SubmitSm sm) // { // sm.UserDataPdu.Headers.Add( // InformationElementIdentifiers.TextFormatting, new byte[] {0, 1, ToByte("00100011")}); // }) // Set ValidityPeriod expired in 2 days .ExpireIn(TimeSpan.FromDays(2)) //Request delivery receipt .DeliveryReceipt(); //Add custom TLV parameter //.AddParameter(0x1403, "free") //Change SubmitSm sequence to your own number. //.Set(delegate(SubmitSm sm) { sm.Sequence = _client.SequenceGenerator.NextSequenceNumber();}) SubmitMode mode = GetSubmitMode(); switch (mode) { case SubmitMode.Payload: builder.MessageInPayload(); break; case SubmitMode.ShortMessageWithSAR: builder.ConcatenationInSAR(); break; } try { IList <SubmitSmResp> resp = await _client.Submit(builder); if (resp.All(x => x.Header.Status == CommandStatus.ESME_ROK)) { _log.Info("Submit succeeded. MessageIds: {0}", string.Join(",", resp.Select(x => x.MessageId))); } else { _log.Warn("Submit failed. Status: {0}", string.Join(",", resp.Select(x => x.Header.Status.ToString()))); } } catch (Exception ex) { _log.Error("Submit failed. Error: {0}", ex.Message); } // When you received success result, you can later query message status on SMSC // if (resp.Count > 0 && resp[0].Status == CommandStatus.ESME_ROK) // { // _log.Info("QuerySm for message " + resp[0].MessageId); // QuerySmResp qresp = _client.Query(resp[0].MessageId, // srcTon, srcNpi,srcAdr); // } }
}//end default constructor #endregion //end region Constructors #region Methods public void SubmitData() { SubmitMode.Submit(new DataSet()); }//end method SubmitData
/// <summary> Prepare SubmitSm instances for sending with methods Submit or SubmitAsync </summary> /// <param name="mode"></param> /// <param name="serviceType"></param> /// <param name="srcTon"></param> /// <param name="srcNpi"></param> /// <param name="srcAddr"></param> /// <param name="destTon"></param> /// <param name="destNpi"></param> /// <param name="destAddr"></param> /// <param name="submitDataCoding"></param> /// <param name="encodeDataCoding"></param> /// <param name="message"></param> /// <returns> List SubmitSm </returns> public List<SubmitSm> PrepareSubmitLarge(SubmitMode mode, string serviceType, byte srcTon, byte srcNpi, string srcAddr, byte destTon, byte destNpi, string destAddr, DataCodings submitDataCoding, DataCodings encodeDataCoding, string message) { List<SubmitSm> submitSmList = new List<SubmitSm>(); if (!SmppBuffer.IsSupportedTextDataCoding(encodeDataCoding)) { OnError(string.Format("Unsupported data coding [{0}]", encodeDataCoding)); encodeDataCoding = DataCodings.Default; } // Standard 160 bytes int maxBytes = ShortMessageMaxBytes; if (encodeDataCoding == DataCodings.UCS2) { // Unicode message maxBytes = 140; } // Convert the message to a byte array byte[] messageBytes = new SmppBuffer(DefaultEncoding, message, encodeDataCoding).Buffer; switch (mode) { case SubmitMode.ShortMessage: if (messageBytes.Length <= maxBytes) { SubmitSm submitSm = SubmitSm.Create(DefaultEncoding, serviceType, srcTon, srcNpi, srcAddr, destTon, destNpi, destAddr); submitSm.DataCoding = submitDataCoding; submitSm.ShortMessageBytes = messageBytes; submitSmList.Add(submitSm); } else { // Subtract 6 bytes for the UDHI header maxBytes = maxBytes - 6; if (encodeDataCoding == DataCodings.Default || encodeDataCoding == DataCodings.ASCII || encodeDataCoding == DataCodings.Latin1 || encodeDataCoding == DataCodings.Latin1Escape || encodeDataCoding == DataCodings.DefaultFlashSMS) { maxBytes = Convert.ToInt32(Math.Floor(Convert.ToDouble(maxBytes) * 8 / 7)); } byte messageReference = SequenceGenerator.ByteCounter; int sequenceNumber = 1; // Split the message in parts we can send List<byte[]> parts = SmppBuffer.SplitMessageOnParts(messageBytes, maxBytes); foreach (byte[] part in parts) { SubmitSm submitSm = SubmitSm.Create(DefaultEncoding, serviceType, srcTon, srcNpi, srcAddr, destTon, destNpi, destAddr); submitSm.DataCoding = submitDataCoding; submitSm.UserData.Headers.AddConcatenatedShortMessages8bit(DefaultEncoding, messageReference, Convert.ToByte(parts.Count), Convert.ToByte(sequenceNumber)); submitSm.ShortMessageBytes = part; submitSmList.Add(submitSm); sequenceNumber++; } } break; case SubmitMode.Payload: if (messageBytes.Length <= maxBytes) { SubmitSm submitSm = SubmitSm.Create(DefaultEncoding, _SystemType, srcTon, srcNpi, srcAddr, destTon, destNpi, destAddr); submitSm.DataCoding = submitDataCoding; submitSm.Optional.AddMessagePayload(messageBytes); submitSmList.Add(submitSm); } else { // Subtract 6 bytes for the UDHI header maxBytes = maxBytes - 6; if (encodeDataCoding == DataCodings.Default || encodeDataCoding == DataCodings.ASCII || encodeDataCoding == DataCodings.Latin1 || encodeDataCoding == DataCodings.Latin1Escape || encodeDataCoding == DataCodings.DefaultFlashSMS) { maxBytes = Convert.ToInt32(Math.Floor(Convert.ToDouble(maxBytes) * 8 / 7)); } byte messageReference = SequenceGenerator.ByteCounter; int sequenceNumber = 1; // Split the message in parts we can send List<byte[]> parts = SmppBuffer.SplitMessageOnParts(messageBytes, maxBytes); foreach (byte[] part in parts) { SubmitSm submitSm = SubmitSm.Create(DefaultEncoding, serviceType, srcTon, srcNpi, srcAddr, destTon, destNpi, destAddr); submitSm.DataCoding = submitDataCoding; submitSm.Optional.AddSARReferenceNumber(Convert.ToUInt16(messageReference)); submitSm.Optional.AddSARSequenceNumber(Convert.ToByte(sequenceNumber)); submitSm.Optional.AddSARTotalSegments(Convert.ToByte(parts.Count)); submitSm.Optional.AddMoreMessagesToSend(sequenceNumber < parts.Count); submitSm.Optional.AddMessagePayload(part); submitSmList.Add(submitSm); sequenceNumber++; } } break; } // All messages need the same validity period foreach (SubmitSm submitSm in submitSmList) { submitSm.ValidityPeriod = DateTime.Now.AddDays(2).ToString("yyMMddhhmmss000+"); } return submitSmList; }
/// <summary> Prepare SubmitMulti instances for sending with methods Submit or SubmitAsync </summary> /// <param name="mode"></param> /// <param name="serviceType"></param> /// <param name="srcTon"></param> /// <param name="srcNpi"></param> /// <param name="srcAddr"></param> /// <param name="destList"></param> /// <param name="submitDataCoding"></param> /// <param name="encodeDataCoding"></param> /// <param name="message"></param> /// <returns> SubmitMulti </returns> public SubmitMultiSm PrepareSubmit(SubmitMode mode, string serviceType, byte srcTon, byte srcNpi, string srcAddr, List<DestinationAddress> destList, DataCodings submitDataCoding, DataCodings encodeDataCoding, string message) { SubmitMultiSm submitMulti = null; if (!SmppBuffer.IsSupportedTextDataCoding(encodeDataCoding)) { OnError(string.Format("Unsupported data coding [{0}]", encodeDataCoding)); encodeDataCoding = DataCodings.Default; } // Standard 160 bytes int maxBytes = ShortMessageMaxBytes; if (encodeDataCoding == DataCodings.UCS2) { // Unicode message maxBytes = 140; } // Convert the message to a byte array byte[] messageBytes = new SmppBuffer(DefaultEncoding, message, encodeDataCoding).Buffer; switch (mode) { case SubmitMode.ShortMessage: submitMulti = SubmitMultiSm.Create(DefaultEncoding, serviceType, srcTon, srcNpi, srcAddr, destList); submitMulti.DataCoding = submitDataCoding; if (messageBytes.Length > maxBytes) { List<byte> memory = new List<byte>(messageBytes); submitMulti.ShortMessageBytes = memory.GetRange(0, 160).ToArray(); } else { submitMulti.ShortMessageBytes = messageBytes; } break; case SubmitMode.Payload: submitMulti = SubmitMultiSm.Create(DefaultEncoding, _SystemType, srcTon, srcNpi, srcAddr, destList); submitMulti.DataCoding = submitDataCoding; submitMulti.Optional.AddMessagePayload(messageBytes); break; } return submitMulti; }