Click here to Skip to main content
Licence CPOL
First Posted 12 Dec 2007
Views 36,770
Bookmarked 52 times

SMS Service

By | 12 Dec 2007 | Article
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

#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)

About the Author

Sunil Scaria

Software Developer
Thomson Reuters
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
QuestionPlease provice Namespace to import PinmemberAsutosha19:09 21 Feb '12  
GeneralProvide code for setup PinmemberNitin Sawant23:20 2 Dec '09  
GeneralGSM Module not Mobile Phone PinmemberBui Tan Duoc20:43 30 Nov '08  
QuestionHow can send the sender name with sms? PinmembersamMaster21:30 23 Sep '08  
AnswerRe: How can send the sender name with sms? PinmemberMember 16464622:12 20 Oct '08  
GeneralRe: How can send the sender name with sms? PinmembersamMaster1:04 21 Oct '08  
GeneralRe: How can send the sender name with sms? PinmemberSunil Scaria19:55 13 Mar '09  
GeneralGSM modem PinmemberJared James Sullivan7:50 13 Dec '07  
AnswerRe: GSM modem PinmemberQssSunil22:49 13 Dec '07  
QuestionTo Multiple numbers? Pinmemberarunjacob19:49 12 Dec '07  
GeneralRe: To Multiple numbers? Pinmember ESTANNY 22:40 12 Dec '07  
GeneralNice Article. PinmemberArun J19:42 12 Dec '07  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 13 Dec 2007
Article Copyright 2007 by Sunil Scaria
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid