Click here to Skip to main content
13,295,051 members (88,441 online)
Click here to Skip to main content
Add your own
alternative version


63 bookmarked
Posted 12 Dec 2007

SMS Service

, 12 Dec 2007
Rate this:
Please Sign up or sign in to vote.
Service for sending SMS through GSM modem


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;

#region Constructor
/// <span class="code-SummaryComment"><summary></span>
/// Constructor for InitializeComponent
/// <span class="code-SummaryComment"></summary></span>

public MySmsService()//Change MySmsService to  your Service Class Name

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

#region OnStop Event of the service
protected override void OnStop()
    EvtlgSmsService.WriteEntry("MySmsService stopped !!.");
    if (serialPort1.IsOpen)

#region Sms Main thread
/// <span class="code-SummaryComment"><summary></span>
/// Sms main thread
/// <span class="code-SummaryComment"></summary></span>

private void SMSThread()
    while (1 == 1);

#region Send Sms function
/// <span class="code-SummaryComment"><summary></span>
/// SendSMS for sending the SMS
/// <span class="code-SummaryComment"></summary></span>

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

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

private void SendSMS(String phoneNumber, String message)
        if (!serialPort1.IsOpen)
        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);

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.


  • 13th December, 2007: Initial post


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
No Biography provided

You may also be interested in...


Comments and Discussions

Questionhi. Pin
Member 1192493324-Aug-15 19:34
memberMember 1192493324-Aug-15 19:34 
QuestionSend sms WORLD wide using Pin
Member 91711288-Oct-12 23:30
memberMember 91711288-Oct-12 23:30 
QuestionPlease provice Namespace to import Pin
Asutosha21-Feb-12 20:09
memberAsutosha21-Feb-12 20:09 
GeneralProvide code for setup Pin
Nitin Sawant3-Dec-09 0:20
memberNitin Sawant3-Dec-09 0:20 
GeneralGSM Module not Mobile Phone Pin
Bui Tan Duoc30-Nov-08 21:43
memberBui Tan Duoc30-Nov-08 21:43 
QuestionHow can send the sender name with sms? Pin
samMaster23-Sep-08 22:30
membersamMaster23-Sep-08 22:30 
AnswerRe: How can send the sender name with sms? Pin
Member 16464620-Oct-08 23:12
memberMember 16464620-Oct-08 23:12 
GeneralRe: How can send the sender name with sms? Pin
samMaster21-Oct-08 2:04
membersamMaster21-Oct-08 2:04 
GeneralRe: How can send the sender name with sms? Pin
Sunil Scaria13-Mar-09 20:55
memberSunil Scaria13-Mar-09 20:55 
Hi dude its possible to that u can specify the sender name .But the name of the sender should be in the phone memory or else replace the sender name with the phone no through your custom logic Smile | :)
GeneralGSM modem Pin
Jared James Sullivan13-Dec-07 8:50
memberJared James Sullivan13-Dec-07 8:50 
AnswerRe: GSM modem Pin
QssSunil13-Dec-07 23:49
memberQssSunil13-Dec-07 23:49 
QuestionTo Multiple numbers? Pin
arunjacob12-Dec-07 20:49
memberarunjacob12-Dec-07 20:49 
GeneralRe: To Multiple numbers? Pin
ESTANNY12-Dec-07 23:40
member ESTANNY 12-Dec-07 23:40 
GeneralNice Article. Pin
Arun J12-Dec-07 20:42
memberArun J12-Dec-07 20: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.

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