Click here to Skip to main content
Click here to Skip to main content

Sending an SMS using ASP.NET & C#

, 9 Jun 2014
Rate this:
Please Sign up or sign in to vote.
Sending an SMS using ASP.NET

Introduction

Nowadays, it is a big question for every novice programmer to develop an application that sends an SMS to any mobile throughout the world. In my first internship, I was asked to develop such an application. I tried & I am finally done.

Using the Code

To use the SMS code is quite simple though it looks quite irritating. I had managed the code in separate files to avoid confusion as well as conflict in using the code.

First of all, I have to develop an interface where the user can put his contact number and message and send it. So for that, I created an interface.

Then it's time to provide SMS functionality to the given interface. For that, I had created sms.cs in the app_code directory as:

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
/// Summary description for SMS
/// </summary>
public class SMS
{
    public SMS()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    private const string sms_username = "username";
    private const string sms_password = "password";
    private const string sms_SenderName = "LMVJAY";
    public string responseFromServer;
    public string PostRequestSMS(string message, string mobileno)
    {
        if (mobileno.Equals(string.Empty))
        {
        }
        else
        {
            string msg = "username=" + sms_username + "&password=" + 
            sms_password + "&sendername=" + sms_SenderName + 
            "&mobileno=" + mobileno + "&message=" + message;

            // Create a request using a URL that can receive a post. 
            //WebRequest request = WebRequest.Create
            ("http://dndopen.dove-sms.com/TransSMS/SMSAPI.jsp?");
            WebRequest request = WebRequest.Create
            ("http://dndopen.dove-sms.com/TransSMS/SMSAPI.jsp?username="+
            sms_username+"&password="+sms_password+"&sendername="+
            sms_SenderName+"&mobileno=" + mobileno + "&message=" + message);
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = msg;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.

            dataStream = response.GetResponseStream();

            // Open the stream using a StreamReader for easy access.
            StreamReader readerst = new StreamReader(dataStream);

            // Read the content.
            responseFromServer = readerst.ReadToEnd();

            readerst.Close();
            // reader.Close();
            dataStream.Close();
            response.Close();
        }
        return responseFromServer;
    }
}

Now, I am ready to call a method PostRequestSms and pass parameter.

So in codebehind on button click event, I fired this code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    SMS objSMS = new SMS();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        objSMS.PostRequestSMS(txtMobNo.Text, txtMessage.Text);
    }
}

Now it's almost done. You have to simply build your project and run and send SMS.

Points of Interest

Sending an SMS via ASP.NET for the first time was a challenge for me. There are various methods to send SMS using ASP.NET, but I find this method somewhat easy.

Before sending an SMS, you have to register an API. I use dove API. So in order to send an SMS, you have to register API.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vijay G. Yadav
Software Developer Appeal Soft Pvt. Ltd.
India India
No Biography provided
Follow on   LinkedIn

Comments and Discussions

 
QuestionWhat things you had used for send SMS? WEB API or what.. PinmemberManoj B. Kalla25-Aug-14 1:35 
GeneralThanks Pinmemberkamlesh suhagiya19-Jun-14 20:53 
GeneralRe: Thanks PinprofessionalVijay G. Yadav22-Jun-14 17:56 
SuggestionIntroduction is lacking Pinmembersobo12311-Jun-14 9:04 
GeneralRe: Introduction is lacking PinprofessionalVijay G. Yadav11-Jun-14 18:36 
QuestionNeed more details PinpremiumSunasara Imdadhusen9-Jun-14 22:57 
AnswerRe: Need more details PinprofessionalVijay G. Yadav9-Jun-14 23:04 
GeneralMy vote of 5 PinmemberHumayun Kabir Mamun9-Jun-14 21:52 
GeneralThanks PinmemberShivraj Choudhary9-Jun-14 21:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 9 Jun 2014
Article Copyright 2014 by Vijay G. Yadav
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid