Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending SMS for a bulk of users

I want to send SMS to bulk of users(4000 user) so i put the following method on loop :

protected int SendSMS(string url)
        {
            // Now to Send Data.

            StreamWriter writer = null;
            StringBuilder postData = new StringBuilder();
            Uri myUri = new Uri(url);
            postData.Append(HttpUtility.ParseQueryString(myUri.Query).Get("Username"));
            postData.Append(HttpUtility.ParseQueryString(myUri.Query).Get("Password"));
            postData.Append(HttpUtility.ParseQueryString(myUri.Query).Get("Sender"));
            postData.Append(HttpUtility.ParseQueryString(myUri.Query).Get("Recipients"));
            postData.Append(HttpUtility.ParseQueryString(myUri.Query).Get("MessageData"));



            string webpageContent = string.Empty;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToString());
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = webRequest.ContentLength = byteArray.Length;

            writer = new StreamWriter(webRequest.GetRequestStream());


            try
            {
                using (Stream webpageStream = webRequest.GetRequestStream())
                {
                    webpageStream.Write(byteArray, 0, byteArray.Length);
                }

                using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
                    {
                        webpageContent = reader.ReadToEnd();
                        //TODO:parse webpagecontent: iF response contains "OK"
                        if (webpageContent.Contains("OK")) return 1;
                        else return 0;
                    }
                }
                //return 1;

            }
            catch (Exception ee)
            {
                ErrMapping.WriteLog(url);
                string error = ee.Message + "<br><br>Stack Trace : " + ee.StackTrace;
                ErrMapping.WriteLog(error);
                return -1;
            }

        }

After a specific number of users like 65 user, no sms had been sent for the rest of users and

I get the following exception :

Error Message:Thread was being aborted.<br><br>Stack Trace :    at System.Net.UnsafeNclNativeMethods.OSSOCK.recv(IntPtr socketHandle, Byte* pinnedBuffer, Int32 len, SocketFlags socketFlags)
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, SocketError& errorCode)
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
   at System.Net.ConnectStream.ProcessWriteCallDone(ConnectionReturnResult returnResult)
   at System.Net.HttpWebRequest.CheckDeferredCallDone(ConnectStream stream)
   at System.Net.HttpWebRequest.GetResponse()
   at SendSMS_EmailUI.Frm_SMS_send.SendSMS(String url) 
like image 908
Anyname Donotcare Avatar asked Nov 28 '22 01:11

Anyname Donotcare


1 Answers

IMHO, the bulk operations that will be performed on behalf of the application can be done easily by the following procedure

  1. When a bulk SMS is triggered, the details will be entered in a database table
  2. A windows service will be constantly monitoring this table for any updates
  3. When the windows service finds the new entries, it will take few records like few hundreds and then send them. Batch Processing.
  4. There can be a delay between consequent requests.
  5. This will ensure you to track which line items have failed and also does not clog the server with the bulk data.

This is a most widely suggested approach.

Please provide your comments on this implementation.

like image 94
Saravanan Avatar answered Dec 06 '22 08:12

Saravanan