Saturday, December 19, 2009

sending sms from asp.net

I use the following two functions in my application to send sms from my asp.net application.
I use c# as programming language.
I provide some credential which I got from the sms API provider. we use www.smscountry.com API in application.
I call the setupSMS function to send sms from my application which requires mobileNumber and message as input parameter and returns the response of the message sent.


    private string setupSMS(string mobileNumber, string message)
    {        
        string strPostBody;        
        String strPostResponse;


        //'Give The Respective Variable Values        
        string userName = "UserId"; //'Enter Your User Name provided by API Provider     
        string password = "Password";//'Enter Your Password provided by API Provider        
        string senderid = "senderId";//' Enter Your Senderid provided by API Provider


        string messageType = "N";//' Enter Your Message Type As N for Normal Message, O for Other Laungauge Sms        
        Char deliveryReports = 'Y';//' if U want To Know Delivery Reports Enter Y it Gives JOBID Otherwise N 


        //'****If U R Behind The Proxy Server Uncomment And Give Below Details *****
        //'objProxy = New WebProxy("Proxy Ip Adress", PortNumber)


        //'Call sendSMS Method For Sending The Sms's
        
        message = Server.UrlEncode(message);        
        strPostBody = "User=" + userName + "&passwd=" + password + "&mobilenumber=" + mobileNumber + "&message=" + message + "&sid=" + senderid + "&mtype=" + messageType + "&DR=" + deliveryReports;
        strPostResponse = sendSMS(strPostBody);
        return strPostResponse;
    }


    private string sendSMS(string stringPost)
    {
        
        HttpWebRequest objWebRequest = null;        
        HttpWebResponse objWebResponse;        
        StreamWriter objStreamWriter = null;        
        StreamReader objStreamReader = null;        
        try
        {
            
            string stringResult;
            
            objWebRequest = (HttpWebRequest)(WebRequest.Create("http://www.smscountry.com/smscwebservice.asp"));
            
            objWebRequest.Method = "POST";
            
            if (objProxy != null)
            {
                objWebRequest.Proxy = objProxy;
            }            
            objWebRequest.ContentLength = stringPost.Length;            
            objWebRequest.ContentType = "application/x-www-form-urlencoded";            
            objStreamWriter = new StreamWriter(objWebRequest.GetRequestStream());            
            objStreamWriter.Write(stringPost);            
            objStreamWriter.Flush();            
            objStreamWriter.Close();
            
            objWebResponse = (HttpWebResponse)(objWebRequest.GetResponse());
            objStreamReader = new StreamReader(objWebResponse.GetResponseStream());
            stringResult = objStreamReader.ReadToEnd();
            objStreamReader.Close();
            return stringResult;  //'jobid
            
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
        finally
        {
            if (objStreamWriter != null)
            {
                objStreamWriter.Close();
            }
            if (objStreamReader != null)
            {
                objStreamReader.Close();
            }
            objWebRequest = null;
            objWebResponse = null;
            objProxy = null;
        }        
    }


hope this post help you in sending SMS through asp.net application.
Enjoy coding.............................................

1 comment: