Click here to Skip to main content
15,878,959 members
Articles / Web Development / ASP.NET
Article

SMS Service

Rate me:
Please Sign up or sign in to vote.
4.71/5 (13 votes)
12 Dec 2007CPOL 84.4K   65   14
Service for sending SMS through GSM modem

Introduction

The article will give you the very basic thing for sending SMS from your computer to any phone over GSM modem.

Using the Code

C#
#region Private Declaration for the main thread
private Thread m_MailThread = null;
#endregion

#region Constructor
/// <summary>
/// Constructor for InitializeComponent
/// </summary>

public MySmsService()//Change MySmsService to  your Service Class Name
{
    InitializeComponent();
}
#endregion

#region OnStart Event of the service
protected override void OnStart(string[] args)
{
    EvtlgSmsService.WriteEntry("MySmsService started!!");
    if (m_MailThread == null)
    m_MailThread = new Thread(new ThreadStart(SMSThread));
    m_MailThread.Start();
}
#endregion

#region OnStop Event of the service
protected override void OnStop()
{
    EvtlgSmsService.WriteEntry("MySmsService stopped !!.");
    if (serialPort1.IsOpen)
    serialPort1.Close();
    m_MailThread.Abort();
}
#endregion

#region Sms Main thread
/// <summary>
/// Sms main thread
/// </summary>

private void SMSThread()
{
    do
    {
        Thread.Sleep(30000);
        SendSMS();
    }
    while (1 == 1);
}
#endregion

#region Send Sms function
/// <summary>
/// SendSMS for sending the SMS
/// </summary>

private void SendSMS()
{
    SendSMS("+91000000000", "Hello This is a test message");
    //+91 is the country code and followed by phone number
}
#endregion

#region Function for sending the Sms
/// <summary>
/// Overloaded send SMS for sending the SMS
/// </summary>
/// <param name="phoneNumber"></param>
/// <param name="message"></param>

private void SendSMS(String phoneNumber, String message)
{
    try
    {
        if (!serialPort1.IsOpen)
        serialPort1.Open();
        serialPort1.Write("AT+CMGF=1" + (Char)13);
        serialPort1.Write(String.Format("AT+CMGS=\"{0}\"" + (Char)13, phoneNumber));
        serialPort1.Write(String.Format("{0}" + (Char)26 + (Char)13, message));
        EvtlgSmsService.WriteEntry("SMS sent successfully");
    }
    catch (Exception e)
    {
        EvtlgSmsService.WriteEntry("SMS sending failed:" + e.Message);
    }
}
#endregion
}

Just cut the above code and paste it in the Service class of your Windows service. Add a serial port component and an event log in the designer. Connect the GSM phone or modem to your computer. Check to which port the modem connects - that port should be given in the SerialPort1.portname. You should also make sure that your modem should support commands in the text mode. Check your modem by using the hyperterminal with the AT commands.

History

  • 13th December, 2007: Initial post

License

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


Written By
Software Developer Thomson Reuters
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhi. Pin
Member 1192493324-Aug-15 18:34
Member 1192493324-Aug-15 18:34 
QuestionSend sms WORLD wide using Asp.net Pin
Member 91711288-Oct-12 22:30
Member 91711288-Oct-12 22:30 
QuestionPlease provice Namespace to import Pin
Asutosha21-Feb-12 19:09
professionalAsutosha21-Feb-12 19:09 
GeneralProvide code for setup Pin
Nitin S2-Dec-09 23:20
professionalNitin S2-Dec-09 23:20 
GeneralGSM Module not Mobile Phone Pin
Bui Tan Duoc30-Nov-08 20:43
professionalBui Tan Duoc30-Nov-08 20:43 
May I use this article with a GSM module such as SIM508?
And some function : InitializeComponent(); , SendSMS("+91000000000", "Hello This is a test message"); where can I find them? Thanks.

if(artical == IDOK)
it's mine;
else
i don't know;

QuestionHow can send the sender name with sms? Pin
samMaster23-Sep-08 21:30
samMaster23-Sep-08 21:30 
AnswerRe: How can send the sender name with sms? Pin
Member 16464620-Oct-08 22:12
Member 16464620-Oct-08 22:12 
GeneralRe: How can send the sender name with sms? Pin
samMaster21-Oct-08 1:04
samMaster21-Oct-08 1:04 
GeneralRe: How can send the sender name with sms? Pin
Sunil Scaria13-Mar-09 19:55
Sunil Scaria13-Mar-09 19:55 
GeneralGSM modem Pin
J Sullivan13-Dec-07 7:50
J Sullivan13-Dec-07 7:50 
AnswerRe: GSM modem Pin
Sunil Scaria13-Dec-07 22:49
Sunil Scaria13-Dec-07 22:49 
QuestionTo Multiple numbers? Pin
Arun Jacob12-Dec-07 19:49
Arun Jacob12-Dec-07 19:49 
GeneralRe: To Multiple numbers? Pin
ESTAN12-Dec-07 22:40
ESTAN12-Dec-07 22:40 
GeneralNice Article. Pin
Arun Jacob12-Dec-07 19:42
Arun Jacob12-Dec-07 19:42 

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

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