Text us or call us on : 011 33 20 30 40
 

Oubound SMS Example C# Code

Example C# Code

Please feel free to copy this code and use it as you please.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Net;
using System.IO;
using System.Net.Mail;
using System.Diagnostics;
using System.Web;

namespace Freedom
{
  namespace SendSMS
  {
    // Class SMSProcess, Used within windows service to send SMS messages from database to AQL 
    // Developed by Garry Pritchard @ Freedom Finance, March 2009 
    // Note:= Needs the Assebley reference System.Web adding to the project and the 
    // using System.web, for the HttpUtility.UrlEncode

    public class SMSProcess
    {
      private string _SMS_ID = "";
      private string _SMS_MSG = "";
      private string _SMS_MOBILE = "";
      private string _DBConnection = global::SendSMS.SMSProcess.Default.DBConnecttion;
      private string _SMSUser = global::SendSMS.SMSProcess.Default.SMSUser;
      private string _SMSPwd = global::SendSMS.SMSProcess.Default.SMSPwd;
      private string _EMailSvr = global::SendSMS.SMSProcess.Default.EmailServer;

      public void ProcessSMS()
      {
        SqlCommand sqlCmd = null;
        SqlConnection sqlCon = null;
        SqlDataReader sqlRead = null;

        // Check to see if we have any new records to process:
        try
        {
          sqlCon = new SqlConnection(this._DBConnection);

          sqlCon.Open();

          sqlCmd = new SqlCommand("SMS_GET_RECORDS", sqlCon);
          sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

          sqlRead = sqlCmd.ExecuteReader();

          while (sqlRead.Read())
          {
            this._SMS_ID = sqlRead["TxtID"].ToString();
            this._SMS_MSG = sqlRead["TxtMsg"].ToString();
            this._SMS_MOBILE = sqlRead["Mobile"].ToString();
            SendMessage();
          }
          sqlRead.Close();
        }
        catch (Exception e)
        {
         LogError("Error in ProcessSMS with message := " + e.Message + " " + e.InnerException);
        }
        finally
        {
          sqlCon.Close();
          sqlCmd.Dispose();
        }
      }

      private void UpdateRecord(string sAQLMSG)
      {
        try
        {
        // Update the record within the db with the response from AQL

        SqlConnection sqlCon = new SqlConnection(this._DBConnection);
        sqlCon.Open();

        SqlCommand sqlCmd = new SqlCommand("SMS_UPDATE_RECORD", sqlCon);
        sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;

        SqlParameter pID = new SqlParameter();
        pID.ParameterName = "@ID";
        pID.Value = Convert.ToInt32(this._SMS_ID);
        pID.DbType = System.Data.DbType.Int32;
        sqlCmd.Parameters.Add(pID);

        SqlParameter pCode = new SqlParameter();
        SqlParameter pMsg = new SqlParameter();

        switch (sAQLMSG)
        {
          case "AQSMS-NOAUTHDETAILS":
            pCode.ParameterName = "@ProcessCode";
            pCode.Value = "E";
            pCode.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pCode);

            pMsg.ParameterName = "@Msg";
            pMsg.Value = "AQSMS-NOAUTHDETAILS";
            pMsg.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pMsg);
            break;
          case "AQSMS-AUTHERROR":
            pCode.ParameterName = "@ProcessCode";
            pCode.Value = "E";
            pCode.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pCode);

            pMsg.ParameterName = "@Msg";
            pMsg.Value = "AQSMS-NOAUTHDETAILS";
            pMsg.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pMsg);
            break;
          case "AQSMS-NOCREDIT":
            pCode.ParameterName = "@ProcessCode";
            pCode.Value = "E";
            pCode.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pCode);

            pMsg.ParameterName = "@Msg";
            pMsg.Value = "AQSMS-NOCREDIT";
            pMsg.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pMsg);
            break;
          case "AQSMS-OK":
            pCode.ParameterName = "@ProcessCode";
            pCode.Value = "Y";
            pCode.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pCode);

            pMsg.ParameterName = "@Msg";
            pMsg.Value = "AQSMS-OK";
            pMsg.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pMsg);
            break;
          case "AQSMS-NOMSG":
            pCode.ParameterName = "@ProcessCode";
            pCode.Value = "Y";
            pCode.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pCode);

            pMsg.ParameterName = "@Msg";
            pMsg.Value = "AQSMS-NOMSG";
            pMsg.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pMsg);
            break;
          case "AQSMS-CREDIT":
            pCode.ParameterName = "@ProcessCode";
            pCode.Value = "Y";
            pCode.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pCode);

            pMsg.ParameterName = "@Msg";
            pMsg.Value = "AQSMS-CREDIT";
            pMsg.DbType = System.Data.DbType.String;
            sqlCmd.Parameters.Add(pMsg);
            break;
        }

        sqlCmd.ExecuteNonQuery();
        sqlCmd.Dispose();
        sqlCon.Close();
      }

      catch (Exception e)
      {
        LogError("Error in UpdateRecord with message := " + e.Message + " " + e.InnerException);
      }
    }

    private void SendMessage()
    {
      try
      {
        string sMsg = this._SMS_MSG;
        sMsg = System.Web.HttpUtility.UrlEncode(sMsg);

        // send the message to aql:
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create
                               ("http://gw1.aql.com/sms/postmsg.php?to_num=" + this._SMS_MOBILE
                                 +"&message=" + sMsg + "&flash=0&originator=MYCOMPANY&username="
                                 +this._SMSUser + "&password=" + this._SMSPwd);

        req.Method = "POST";
        req.KeepAlive = false;
        req.ContentType = "application/x-www-form-urlencoded";
        // Get the request stream.
        // Get the response.
        WebResponse response = req.GetResponse();
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        UpdateRecord(responseFromServer.ToUpper());

        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
      }
      catch (Exception e)
      {
        LogError("Error in SendMessage with message := " + e.Message + " " + e.InnerException);
      }
    }

    private void LogError(string sMessage)
    {
      // TODO SEND & LOG Error Msg
      System.Net.Mail.MailMessage oMail = new System.Net.Mail.MailMessage();
      oMail.To.Add(new MailAddress("support@mycompany.com"));
      oMail.From = new MailAddress("myemail@mycompany.com");
      oMail.Subject = "SendSMS App Error:";
      oMail.Body = sMessage;

      SmtpClient oSMTP = new SmtpClient();
      oSMTP.Host = this._EMailSvr;
      oSMTP.Send(oMail);

      EventLog.WriteEntry("Send SMS Error", sMessage);
    }
  }
}
}

Quick Links

Latest News


aql gain ISO 27001 Accreditation

aql Operations Director,...

Read More

aql's historic head office - the Salem Chapel

(Right to...

Read More

aql join Datacentre Alliance

...

Read More

IXLeeds to put Leeds on the 'Internet Map'

...

Read More

JANET 3G - First feedback by St Andrews University

It's always fantastic to receive customer feedback (whether good or bad). The team at aql were...

Read More