Click here to Skip to main content
11,930,048 members (45,915 online)
Click here to Skip to main content
Add your own
alternative version


266 bookmarked

Sending SMS using .NET

, 11 Jun 2007 CPL
Rate this:
Please Sign up or sign in to vote.
This article covers some of the ways of sending SMS using .NET


When do you want to send SMS via applications? There could be plethora of use cases for this. The simplest one is to validate a mobile number, and some of the complicated ones could involve sending an SMS after a huge workflow is complete or gone wrong. Let's find out ways to send SMS using C#/VB.NET.

Sending SMS

What are the ways in which one can send SMS?

  • Using a GSM modem:
    • Better when one wants to implement offline applications and a very small number of SMS go every minute, usually few 10s.
  • Using web service:
    • Better when it is an online application and a very few number of SMS go every minute, usually few 10s.
  • Using endpoints given by service the provider:
    • Better when the number of SMS exceeds a few 100s per minute. Service provider demands a commitment of at least 100,000 SMS per month.

Sending SMS via a webservice or endpoints is simplest. In contrast, sending SMS via GSM modem has a few additional steps to take care of. Let's understand each in detail.

Sending SMS via GSM modem

  1. First, find the best GSM modem that suits the needs. Specifications are available here.
  2. Understand the AT Command set required to communicate with the modem.
  3. Connect the modem to the computer according to the setup guide specified in the manual provided with the GSM modem. Sample connection details of the Maestro 20/100 modem can be found here. The connection settings explained are common for most GSM modems.
  4. Create a new Windows application or Web Application.
  5. Add a new class file with the name SMSCOMMS.
  6. Copy and paste the code given below into the class.

Coding with VB.NET

If you are using VB.NET for coding the application, you can use the class shared by Jeanred. Details are as follows:

Option Explicit On    

Imports System
Imports System.Threading
Imports System.ComponentModel
Imports System.IO.PortsPublic Class SMSCOMMS    

Private WithEvents SMSPort As SerialPort    

    Private SMSThread As Thread
    Private ReadThread As Thread
    Shared _Continue As Boolean = False
    Shared _ContSMS As Boolean = False
    Private _Wait As Boolean = False
    Shared _ReadPort As Boolean = False
    Public Event Sending(ByVal Done As Boolean)
    Public Event DataReceived(ByVal Message As String)    

    Public Sub New(ByRef COMMPORT As String)
        SMSPort = New SerialPort
        With SMSPort
            .PortName = COMMPORT
            .BaudRate = 9600
            .Parity = Parity.None
            .DataBits = 8
            .StopBits = StopBits.One
            .Handshake = Handshake.RequestToSend
                .DtrEnable = True
              .RtsEnable = True
            .NewLine = vbCrLf
        End With
        ReadThread = New Thread(AddressOf ReadPort)
        End Sub    

    Public Function SendSMS(ByVal CellNumber As String, 
        ByVal SMSMessage As String) As Boolean
        Dim MyMessage As String = Nothing
        'Check if Message Length <= 160
        If SMSMessage.Length <= 160 Then
            MyMessage = SMSMessage
              MyMessage = Mid(SMSMessage, 1, 160)
        End If
        If IsOpen = True Then
                SMSPort.WriteLine("AT+CMGS=" & CellNumber & vbCr)
                _ContSMS = False
                SMSPort.WriteLine(MyMessage & vbCrLf & Chr(26))
                _Continue = False
                RaiseEvent Sending(False)
        End If
    End Function    

    Private Sub ReadPort()
          Dim SerialIn As String = Nothing
        Dim RXBuffer(SMSPort.ReadBufferSize) As Byte
        Dim SMSMessage As String = Nothing
        Dim Strpos As Integer = 0
        Dim TmpStr As String = Nothing    

            While SMSPort.IsOpen = True
            If (SMSPort.BytesToRead <> 0) And (
                SMSPort.IsOpen = True) Then
                While SMSPort.BytesToRead <> 0
                    SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize)
                    SerialIn = 
                        SerialIn & System.Text.Encoding.ASCII.GetString(
                    If SerialIn.Contains(">") = True Then
                        _ContSMS = True
                    End If
                    If SerialIn.Contains("+CMGS:") = True Then
                        _Continue = True
                        RaiseEvent Sending(True)
                        _Wait = False
                        SerialIn = String.Empty
                        ReDim RXBuffer(SMSPort.ReadBufferSize)
                    End If
                End While
                RaiseEvent DataReceived(SerialIn)
                SerialIn = String.Empty
                ReDim RXBuffer(SMSPort.ReadBufferSize)
            End If
        End While
    End Sub    

    Public ReadOnly Property IsOpen() As Boolean
            If SMSPort.IsOpen = True Then
                IsOpen = True
                IsOpen = False
            End If
        End Get
    End Property    

    Public Sub Open()
        If IsOpen = False Then
        End If
    End Sub    

    Public Sub Close()
        If IsOpen = True Then
        End If
    End Sub    

End Class

The above class exposes three functions: Open, SendSMS and Close. While creating the instance of the class, provide the port to which the modem is connected. In a Windows application, follow the steps below:

SMSEngine = New SMSCOMMS("COM1")
SMSEngine.SendSMS("919888888888","SMS Testing")

Coding with C#

The C# implementation of the code is as follows:

using System;
using System.Threading;
using System.ComponentModel;
using System.IO.Ports;   

public class SMSCOMMS
    private SerialPort SMSPort;
    private Thread SMSThread;
    private Thread ReadThread;
    public static bool _Continue = false;
    public static bool _ContSMS = false;
    private bool _Wait = false;
    public static bool _ReadPort = false;
    public delegate void SendingEventHandler(bool Done);
    public event SendingEventHandler Sending;
    public delegate void DataReceivedEventHandler(string Message);
    public event DataReceivedEventHandler DataReceived;    

    public SMSCOMMS(ref string COMMPORT)
        SMSPort = new SerialPort();
        SMSPort.PortName = COMMPORT;
        SMSPort.BaudRate = 9600;
        SMSPort.Parity = Parity.None;
        SMSPort.DataBits = 8;
        SMSPort.StopBits = StopBits.One;
        SMSPort.Handshake = Handshake.RequestToSend;
        SMSPort.DtrEnable = true;
        SMSPort.RtsEnable = true;
        SMSPort.NewLine = System.Environment.NewLine;
        ReadThread = new Thread(
            new System.Threading.ThreadStart(ReadPort));

    public bool SendSMS(string CellNumber, string SMSMessage)
        string MyMessage = null;
        //Check if Message Length <= 160
        if (SMSMessage.Length <= 160)
            MyMessage = SMSMessage;
            MyMessage = SMSMessage.Substring(0, 160);
        if (IsOpen == true)
            SMSPort.WriteLine("AT+CMGS=" + CellNumber + "r");
            _ContSMS = false;
                MyMessage + System.Environment.NewLine + (char)(26));
              _Continue = false;
            if (Sending != null)
        return false;

    private void ReadPort()
        string SerialIn = null;
        byte[] RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
        string SMSMessage = null;
        int Strpos = 0;
        string TmpStr = null;
        while (SMSPort.IsOpen == true)
            if ((SMSPort.BytesToRead != 0) & (SMSPort.IsOpen == true))
                while (SMSPort.BytesToRead != 0)
                    SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize);
                    SerialIn = 
                        SerialIn + System.Text.Encoding.ASCII.GetString(
                            if (SerialIn.Contains(">") == true)
                        _ContSMS = true;
                    if (SerialIn.Contains("+CMGS:") == true)
                        _Continue = true;
                        if (Sending != null)
                        _Wait = false;
                        SerialIn = string.Empty;
                        RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
                if (DataReceived != null)
                SerialIn = string.Empty;
                RXBuffer = new byte[SMSPort.ReadBufferSize + 1];

    public bool SendSMS(string CellNumber, string SMSMessage)
        string MyMessage = null;
        if (SMSMessage.Length <= 160)
            MyMessage = SMSMessage;
            MyMessage = SMSMessage.Substring(0, 160);
        if (IsOpen == true)
            SMSPort.WriteLine("AT+CMGS=" + CellNumber + "r");
            _ContSMS = false;
                    MyMessage + System.Environment.NewLine + (char)(26));
              _Continue = false;
            if (Sending != null)
        return false;

    public void Open()
        if (IsOpen == false)

    public void Close()
        if (IsOpen == true)


Then use the code as below:

SMSEngine = new SMSCOMMS("COM1");
SMSEngine.SendSMS("919888888888","THIS IS YOUR MESSAGE");

Sending SMS via a webservice

Sending SMS via webservices, although not for real-time services, is a very cost-effective solution. There are lots of webservices and you should be able to find one by searching the web. There are free ones that are not so reliable. So, purchase SMS credits to send a limited number of SMS using a webservice. Here, the usage is very simple, as it is just consuming a webservice to send a number and message to a function. The Code Project sample is available at The Code Project.

Sending SMS via service provider endpoints

Sending SMS via service provider is also similar to using a webservice. Here, it may be a non-standard protocol or over HTTP. It differs from service provider to service provider. Some provide sample code that can be used for programming custom applications.


  • 1 June, 2007 -- Original version posted.
  • 11 June, 2007 -- Article edited and posted to the main article base.


This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


About the Author

India India

You may also be interested in...

Comments and Discussions

Questionsend a message without taking the balance or using webservice Pin
Member 121278319-Nov-15 21:39
memberMember 121278319-Nov-15 21:39 
QuestionHow to send & receive USSD code? Pin
Hoàng Thanh Phạm9-Nov-15 17:24
memberHoàng Thanh Phạm9-Nov-15 17:24 
QuestionHoe to initialize smsengine any ideas? Pin
Ritin Jain9-Oct-15 1:41
memberRitin Jain9-Oct-15 1:41 
QuestionHandling events Pin
Applez0080026-May-15 21:06
memberApplez0080026-May-15 21:06 
Questionconvert this into a DLL?? Pin
Member 1164086827-Apr-15 3:07
memberMember 1164086827-Apr-15 3:07 
AnswerRe: convert this into a DLL?? Pin
omar_chavez13-Aug-15 14:24
memberomar_chavez13-Aug-15 14:24 
Member 1034097619-Mar-15 20:55
memberMember 1034097619-Mar-15 20:55 
QuestionHow to send multipart long SMS using gsm modem in C# Pin
Mohamed Farhan17-Mar-15 12:15
memberMohamed Farhan17-Mar-15 12:15 
AnswerIt is okay to send SMS Pin
loukt6-Nov-14 22:08
memberloukt6-Nov-14 22:08 
Questioncom not detected Pin
Member 1112453614-Oct-14 19:00
memberMember 1112453614-Oct-14 19:00 
QuestionIt works now Pin
niranjanbhuta200612-May-14 2:24
memberniranjanbhuta200612-May-14 2:24 
QuestionYour code is not working Pin
Questionhi Pin
srinivaspitla6622-Nov-13 9:11
membersrinivaspitla6622-Nov-13 9:11 
QuestionNot sending SMS Pin
Raheel Ahmad10-Oct-13 0:53
memberRaheel Ahmad10-Oct-13 0:53 
QuestionSMS is not receiving on my mobile. Pin
Raheel Ahmad9-Oct-13 4:43
memberRaheel Ahmad9-Oct-13 4:43 
SuggestionRepetetion Removal Pin
Sami Ciit27-Aug-13 22:01
memberSami Ciit27-Aug-13 22:01 
QuestionSend Sms Pin
shibashish mohanty19-Jul-13 4:55
membershibashish mohanty19-Jul-13 4:55 
QuestionDoes not work in Wavecom GSM modem. Pin
JMAM14-Jun-13 9:12
memberJMAM14-Jun-13 9:12 
QuestionMessage Service error 500 Pin
Le@rner2-May-13 2:59
memberLe@rner2-May-13 2:59 
Questionauto detect com port Pin
coderpavel19-Apr-13 7:43
membercoderpavel19-Apr-13 7:43 
Questioninteligent menu Pin
parasqqqqqqqq6-Apr-13 0:15
memberparasqqqqqqqq6-Apr-13 0:15 
AnswerRe: inteligent menu Pin
parasqqqqqqqq6-Apr-13 0:18
memberparasqqqqqqqq6-Apr-13 0:18 
QuestionCMS Error 304 Pin
Kodeeswaran V Duraisamy25-Mar-13 21:07
memberKodeeswaran V Duraisamy25-Mar-13 21:07 
QuestionNott working Pin
tuncay temurlenk19-Mar-13 14:23
membertuncay temurlenk19-Mar-13 14:23 
Questiondo you have 2008 codes? Pin
Gary Roland25-Jan-13 8:24
memberGary Roland25-Jan-13 8:24 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 11 Jun 2007
Article Copyright 2007 by PooranPrasad
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid