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 Post49 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

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

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

    http://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 sir_kavin@hotmail.com

  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 welan_choo_2@hotmail.com

  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

  30. Rabin | Jan 28, 2009 | Reply

    Hello Sir,

    I’m interesting in this library. Can this library works on ASP.NET?

    Thank you very much!

  31. okpamen kelly | Feb 23, 2009 | Reply

    i want to know more

  32. waleed | Mar 7, 2009 | Reply

    hi every body
    i wnt to send and recive sms when i have ip for mobile company (gsm,cdma)
    i hope to help my
    plz…
    by

  33. Jp | Apr 3, 2009 | Reply

    Hi,
    I am using Nokia 3110 Classic to send SMS. Unable to send sms using the code. May i know what are all the tested versions?

    Thanks,
    Jp

  34. Vu Tam | Apr 4, 2009 | Reply

    I can’t send sms. I use cellphone to connect to my computer by USB cable. And ERROR: “Error sending message to . Error sending SMS message: Unknown exception in sending command”. Help me.

  35. Namran Ahmed | Apr 6, 2009 | Reply

    Hi guys ..
    Thanks for you support .
    I have one problem with this ATSMS.DLL that after sending or receiving SMS I want to Clear my memory like AT+CMGD ..
    How I can do this using this ATSMS.DLL

  36. gLin | Apr 7, 2009 | Reply

    hey. i got this error when i start to send the message out.. saying
    Error sending SMS message: Object reference not set to an instance of an object.
    And it’s higlight this sentence

    msgNo = modem.SendSMS(txtPhone.Text, msg, Common.EnumEncoding.GSM_Default_7Bit)

  37. gLin | Apr 7, 2009 | Reply

    can anyone please help me? i need complete it by this week

    PLEASE HELP ME!

  38. Anil | May 15, 2009 | Reply

    Hi everyone … If you want the code to work better use sonyericsson phone and make sure how your connecting the phone by cable/bluetooth … The comm port for bluetooth has to enabled so that it can send sms through comm ports

  39. Anil | May 15, 2009 | Reply

    Please check the comm port connections this will solve the issue
    Can i know which handset/gsm modem are you using to send sms

  40. Ivan | May 29, 2009 | Reply

    Hello everyone i’m doing a project on something similar to this website http://www.nus.edu.sg/comcen/sms/exams.htm.Can u guys teach me how to do it course i’m really stuck with it.Much thanks to guys out there.

  41. Ivan | May 29, 2009 | Reply

    Hello everyone i’m doing a project on something similar to this website http://www.nus.edu.sg/comcen/sms/exams.htm.

    Can u guys teach me how to do it course i’m really stuck with it.Much thanks to guys out there.

  42. NATHAN | Jul 1, 2009 | Reply

    hello i need help for 5 years child his name is thomas tamil selvan who is brilliant but he is not having moneyto study if any body can take care for his studies we will be great full to them his father left him when he was a born baby his mother working as a house maid and maintaining him now he have to study 1 st standard because of not paying the amount they send him out of the school please do the need full to this small child

  43. ocealseoscifs | Sep 27, 2009 | Reply

    I think you made some good points in your post.

  44. kholiq | Nov 22, 2009 | Reply

    sir, on my windows XP I can send and received SMS but I can’t received on windows server
    I use nokia 6230, what is the problem??

  45. Sunil | Nov 24, 2009 | Reply

    I am using Nokia 5130 xpress music. Connection is okay but, when I try to send sms, it is saying “Object refernce not set to instance of an object”

    Trying with your demo code, please help me out

  46. balaji darphalkar | Dec 24, 2009 | Reply

    I’m using Nokia 6303 classc mobile with USB Cable in COM4, but whenever trying to

    connect phone than getting message. Connected phone sucessfully.

    boud rate = ?
    data bit= ?
    stop bit = ?
    flow control =?

    then send sms then message ststus

    ‘message queued for sending ‘

    then msgbox ok

    “Error sending message to . Error sending SMS message: Unknown exception in sending command”

    I hope you can

    understand my problem. please let me know as soon as possible.

  47. balaji darphalkar | Dec 24, 2009 | Reply

    Error sending message to . Error sending SMS message: Object reference not set to an instance of an object.

    please let me know as soon as possible.

  48. Arul | Feb 15, 2010 | Reply

    I have error when i sent the SMS using your DLL. The following error will be display.

    “Error Sending Message to. Errpr Sending SMS Message Unknow exception in sending command.

    Pl help me sir.

  49. Humayun | Feb 28, 2010 | Reply

    Hi,

    Sir, i wanted to know that what do you mean the mobile is connected or not, does it mean your mobile is connected with PC Suite?? pl reply on my email address, Thankyou most kindly in advance for your kind help …

    Respectfully,
    humayun

Sorry, comments for this entry are closed at this time.