RSS Feed for This PostCurrent Article

Send SMS through GMS modem or CellPhone using .NET SMS Library


Download Source Code

In order to send SMS, your mobile phone must support the AT+CMGF command. AT+CMGF is used to set the message format for the SMS message. The values can be either 0 (PDU message) or 1 (text message). To know if your mobile phone support the AT+CMGF command, you can issue commands directly using HyperTerminal to check the support of the command.

cmgf.jpg

AT+CMGF? returns the current message format, AT+CMGF=? returns the supported values for your handset. The SMS library supports both modes. However, in order to send Unicode SMS, PDU encoded SMS message must be used. If your mobile phone does not support PDU mode, then there is no way you can send Unicode SMS.

Let’s start to have fun. To send SMS using the library, it is very straight forward. After connecting to your handset, just call the SendSMS API and pass in the phone number, message and the encoding, which can either be in GSM default 7 bit or Unicode 16 bit. There are other encodings in the library like Class 2 7 bit, Class 2 8 bit, and hex message but mostly we will only use the common encodings, which are GSM default 7 bit and Unicode 16 bit.

phonesms.jpg

StringUtils.IsUnicode checks if your message is ASCII or Unicode and calls the SendSMS API using the correct encoding.

NOTE: In the code I also showed you the total SMS sent versus the total SMS that can be sent. By default the SMS message sent is stored in the SIM memory. SIM memory only can store limited amount of SMS. If the maximum is reached, you will not be able to send anymore SMS, instead exception will be thrown. It is very important you clear up all the SMS messages in the memory. You can achieve this by using the SMS APIs provided.

Imports ATSMS
Public Class MainForm
Private oGsmModem As New GSMModem

Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnPhone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click

If cboComPort.Text = String.Empty Then

MsgBox(”COM Port must be selected”, MsgBoxStyle.Information)

Return

End If

oGsmModem.Port = cboComPort.Text

If cboBaudRate.Text <> String.Empty Then

oGsmModem.BaudRate = Convert.ToInt32(cboBaudRate.Text)

End If

If cboDataBit.Text <> String.Empty Then

oGsmModem.DataBits = Convert.ToInt32(cboDataBit.Text)

End If

If cboStopBit.Text <> String.Empty Then

Select Case cboStopBit.Text

Case “1″

oGsmModem.StopBits = Common.EnumStopBits.One

Case “1.5″

oGsmModem.StopBits = Common.EnumStopBits.OnePointFive

Case “2″

oGsmModem.StopBits = Common.EnumStopBits.Two

End Select

End If

If cboFlowControl.Text <> String.Empty Then

Select Case cboFlowControl.Text

Case “None”

oGsmModem.FlowControl = Common.EnumFlowControl.None

Case “Hardware”

oGsmModem.FlowControl = Common.EnumFlowControl.RTS_CTS

Case “Xon/Xoff”

oGsmModem.FlowControl = Common.EnumFlowControl.Xon_Xoff

End Select

End If

Try

oGsmModem.Connect()

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.Critical)

Return

End Try

btnSend.Enabled = True

btnDisconnect.Enabled = True

MsgBox(”Connected to phone successfully !”, MsgBoxStyle.Information)

End Sub

Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click

Try

oGsmModem.Disconnect()

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.Critical)

End Try

btnSend.Enabled = False

btnDisconnect.Enabled = False

btnConnect.Enabled = True

End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

If txtPhoneNumber.Text.Trim = String.Empty Then

MsgBox(”Phone number must not be empty”, MsgBoxStyle.Critical)

Return

End If

If txtMsg.Text.Trim = String.Empty Then

MsgBox(”Phone number must not be empty”, MsgBoxStyle.Critical)

Return

End If

Try

Dim msg As String = txtMsg.Text.Trim

Dim msgNo As String

If StringUtils.IsUnicode(msg) Then

msgNo = oGsmModem.SendSMS(txtPhoneNumber.Text, msg, Common.EnumEncoding.Unicode_16Bit)

Else

msgNo = oGsmModem.SendSMS(txtPhoneNumber.Text, msg, Common.EnumEncoding.GSM_Default_7Bit)

End If

MsgBox(”Message is sent. Reference no is ” & msgNo, MsgBoxStyle.Information)

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.Critical)

End Try

Try

Dim storages() As Storage = oGsmModem.GetStorageSetting

Dim i As Integer

txtStorage.Text = String.Empty

For i = 0 To storages.Length - 1

Dim storage As Storage = storages(i)

txtStorage.Text += storage.Name & “(” & storage.Used & “/” & storage.Total & “), ”

Next

Catch ex As Exception

txtStorage.Text = “Not supported”

End Try

End Sub

End Class


Trackback URL


RSS Feed for This Post29 Comment(s)

  1. bisal tiwari | Sep 18, 2007 | Reply

    send only the message

  2. Prashant | Oct 12, 2007 | Reply

    Hi,

    I want know on which Mobile handset have you tested the application

  3. admin | Oct 12, 2007 | Reply

    It has been tested on certain model of Nokia and Sony Ericsson phones. I will compile a list and publish on the website later when version 2 is ready.

  4. Alejandro | Oct 29, 2007 | Reply

    What is GSMModem at the beginin of code? Where is defined or is a specific dll?

    Thanks.

    Alejandro

  5. mary | Dec 15, 2007 | Reply

    i DL the source code that have a link , but i cant run the demo , whats wrong with me?
    i need to write a program that send and recive msg by PC please help me to know what i must to do?

  6. joe | Dec 20, 2007 | Reply

    help me out pliz.i’m using a nokia E50 as a modem and connecting successfully to it.iwant to send and receive sms using the .Net sms library but i keep getting an error message.

  7. Abhinav | Jan 8, 2008 | Reply

    sir, i want to know how i can make my SMS gateway that works as other SMS gateway.Please send Me full Process at my Email ID.

  8. Sharone | Jan 26, 2008 | Reply

    Sir,
    I am doing a project on bulk sms using GSM modem. Can u please guide me on this…

  9. masdoud | Jan 27, 2008 | Reply

    love u thanks you helped me sooo much

  10. Sharone | Jan 29, 2008 | Reply

    The source code on bulk sms using GSM gives error message: unknown exception.. Can u plz guide me on this.

  11. admin | Jan 29, 2008 | Reply

    Can u give the model of the phone u r using ?

  12. Sharone | Jan 30, 2008 | Reply

    i used nokia 7610.. It was successfully connected.. Later when sending an sms it gives an error..

  13. admin | Jan 30, 2008 | Reply

    Can you check if your phone support AT+CMGF. Refer to the following

    https://twit88.com/blog/2007/09/
    16/send-sms-through-gms-
    modem-or-cellphone-using-net-sms-library/

    https://twit88.com/blog/2007/09/22/
    use-the-net-sms-library-to-
    retrieve-phone-settings/

    https://twit88.com/blog/2007/10/30/
    know-your-phone-better-programmatically/

  14. phia | Mar 6, 2008 | Reply

    sir can you help guide me on this, i’m trying to write a program to send SMS to a GSM/GPS modem. and from there have the modem send it somewhere else. Can you get me some hint on where to start doing the program? please thankyou sir,

    my email adress is [email protected]

  15. mohand | Mar 13, 2008 | Reply

    hllo

  16. senjahitam | Mar 15, 2008 | Reply

    i try this program but i can’t send a message.
    i used phone Sony Ericsson K610i

    thanks.

    regards,

  17. Mahmoud | Mar 30, 2008 | Reply

    I used N82 and i couldn’t send the message because of unknown error.
    I also tried to send the message from Hyber terminal and it fails but whe i set the text message mode AT+CMGF=1 it sends the message and everything works.
    So can you please tell me how to do the using the Open Sourse liberary

  18. murugesh | Apr 12, 2008 | Reply

    hai every body

  19. Idayathullah | May 2, 2008 | Reply

    i use sony ericsson k550i, i could not send sms
    i did not receive any errors

  20. tumptrirush | May 5, 2008 | Reply

    Surprise your boss. Get to work on time.

    —————————————————————————————————-
    http://xanga.com/robbiesteelegk

  21. Ephrem Bekele | May 22, 2008 | Reply

    Hi Ephrem How are u, and every thing?
    For a long time I try to get phone but in this week there is no network connection , b/c of Telecommunication internet café stop to daily phone.
    and in Saturday I got in my phone one number but when I called the person can’t listen me so if I get the network I call u today

    by yetnayet

  22. Sharone | May 30, 2008 | Reply

    Im using nokia 7640 mobile phone and am getting an error message when click on the send button even though the phone is connected successfully.. can u plz guide me on that..

  23. Sharone | May 30, 2008 | Reply

    Im using nokia 7610 mobile phone and am getting an error message when click on the send button even though the phone is connected successfully.. can u plz guide me on that.. sorry for the previous comment i made3 a mistake when writing the phone no.

  24. FarBalayabede | May 31, 2008 | Reply

    Hello, my friend!
    Please, don’t hate me, I just want to earn some money :)
    See you!
    Buy phentermine here!

  25. Manohar | Jul 31, 2008 | Reply

    hello sir,
    i used ur code,my handset is Nokia Xpress music edition ,i check it compatible for At commands and it support.my handset also connected successfully .but when i send SMS it throw error “Object Reference not set” on to sendsms method.

    please give me solution…

    thanks in Advance

  26. Manohar | Jul 31, 2008 | Reply

    if possible ,Please send me the solution on my mail id

  27. Welan | Sep 11, 2008 | Reply

    I try out your demo but i got lots of error.
    Im trying to send out sms text from my pc to GSM modem to my handphone using VS.

    can u kindly help me?
    my email is [email protected]

  28. dharmesh | Oct 21, 2008 | Reply

    i have to make push pull sms application in java.plz guide.
    thanks

  29. sledge | Dec 11, 2008 | Reply

    hello

    is there a way to send a wave file if you have dialed a phone? I want to implement some text reading system on a phone.
    tia, i looked a lot on the internet but no luck so far…

    mario

RSS Feed for This PostPost a Comment