Your personal SMS Gateway with C#

Your personal SMS Gateway with C#

Today I will describe simple but effective way to send sms from your computer using your mobile phone and a short C# application. I’m using Nokia 6070 but I guess any phone which has the ability to send SMS through AT commands will do.
First of all you would have to have a mobile phone connected to the computer through a cable and a driver for this cable installed. You need a driver to transfrom your usb cable into a serial interface.
To check if your phone is able to send sms using AT Commands please visit the following link and test it through the hyper terminal.
Once this is working you can easily do the same using C# and SerialPort class.
Here is a complete source which will transform your computer into your personal SMS Gateway:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;

namespace SMSSender
{
    class Program
    {
        static SerialPort p9 = new SerialPort("COM9", 9600);
        static void Main(string[] args)
        {
            p9.DataReceived += new SerialDataReceivedEventHandler(p9_DataReceived);
            p9.Open();
            p9.Write("ATrn");
            Thread.Sleep(500);
            p9.Write("AT+CMGF=1rn");
            Thread.Sleep(500);
            p9.Write("AT+CMGS="123456789"rn");
            Thread.Sleep(500);
            p9.Write("!personal gateway!n");
            Thread.Sleep(500);
            p9.Write("Hello Worldx1Arn");
            p9.Close();
        }

        static void p9_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                Console.WriteLine("Data received.." + p9.ReadLine());

            }catch(Exception ex){

            }
        }
    }
}

“123456789” stands for the mobile phone number where you want to send SMS to, and ‘x1A’ is a replacement for + which you have to issue when you want to send sms on the GSM modem.

Enjoy!

5 responses on “Your personal SMS Gateway with C#

  1. Varun Digumber September 1, 2010 at 6:55 pm

    p9.Write(“AT+CMGS=”123456789″rn”);
    I am getting error here! u cant use 2 set of double quotes here.suggest please

  2. admin September 1, 2010 at 8:10 pm

    There must be a problem with my code formatting, as the error description says, you either have to concatenate the string or escape the quotes

  3. vijitha October 8, 2010 at 10:36 am

    hello

    I am oing website with sms gateway.I would like to develop my own gateway.Any one have idea about this means pls share with me.

  4. Wasi August 8, 2011 at 1:47 pm

    You need to concatenate the both strings, as :
    p9.Write(“AT+CMGS=”+123456789+”rn”);

    then it should work fine.

  5. SmS Marketer August 20, 2011 at 12:05 pm

    nicely put my friend , i wish i could of came up with such an eliquent way of putting it

Leave a Reply