RSS Feed for This PostCurrent Article

How to Receive SMS using .NET SMS Library

Download Source Code

To detect new SMS received, it is a bit tricky as different mobile phones from different manufacturers may need different configurations.

In order to detect incoming SMS, you must use the “AT+CNMI” command to set the new message indication to the terminal equipment (TE).

cnmi.jpg

For the “+CNMI” command, the values are in the format of mode,mt,bm,ds,bfr. In order to receive SMS, mt must be 1. For other values, different handsets may require different values to be set. By default, the SMS library sets mode and mt to 1 which should be sufficient for most handsets. However, you can always use the CheckATCommands function to check your phone supported values.

Take note also that during my testing, it was noticed that for certain Nokia phones only, Class 2 SMS can be detected. For newer models of Nokia phones, e.g., N70, N80, the “+CNMI” is not supported, and you have no way of detecting incoming SMS.

phonesmsrecv.jpg

By setting NewMessageIndication to True, and if your phone supports “+CNMI”, the NewMessageReceived event should be raised.

Imports ATSMS

Public Class MainForm

Private WithEvents oGsmModem As New GSMModem

Private Sub MainForm_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CheckForIllegalCrossThreadCalls = False
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

Try
oGsmModem.NewMessageIndication = True
Catch ex As Exception

End Try

btnSendMsg.Enabled = True
btnSendClass2Msg.Enabled = True
btnCheckPhone.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

btnSendMsg.Enabled = False
btnSendClass2Msg.Enabled = False
btnCheckPhone.Enabled = False
btnDisconnect.Enabled = False
btnConnect.Enabled = True

End Sub

Private Sub btnSendMsg_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSendMsg.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 & “. Make sure your SIM memory” & _
” is not full.”, 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

Private Sub btnSendClass2Msg_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnSendClass2Msg.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.Class2_7_Bit)
End If
MsgBox(“Message is sent. Reference no is “ & msgNo, _
MsgBoxStyle.Information)
Catch ex As Exception
MsgBox(ex.Message & “. Make sure your SIM memory is not full.”, _
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

Private Sub oGsmModem_NewMessageReceived(ByVal e As _
ATSMS.NewMessageReceivedEventArgs) Handles _
oGsmModem.NewMessageReceived
txtMsg.Text = “Message from “ & e.MSISDN & “. Message – “ & _
e.TextMessage & ControlChars.CrLf
End Sub

Private Sub btnCheckPhone_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCheckPhone.Click
MsgBox(“Going to analyze your phone. It may take a while”, _
MsgBoxStyle.Information)
oGsmModem.CheckATCommands()
If oGsmModem.ATCommandHandler.Is_SMS_Received_Supported Then
MsgBox(“Your phone is able to receive SMS. Message “ & _
“indication command is “ & _
oGsmModem.ATCommandHandler.MsgIndication, _
MsgBoxStyle.Information)
oGsmModem.NewMessageIndication = True
Else
MsgBox(“Sorry. Your phone cannot receive SMS”, _
MsgBoxStyle.Information)
End If
End Sub
End Class


Trackback URL


RSS Feed for This Post36 Comment(s)

  1. OpenSourcer | Sep 29, 2007 | Reply

    Hi! thank you for providing this SMS Library, it helps a lot easier when developing SMS based application.
    But I get this exception when receiving an SMS.

    ATSMS.InvalidOpException was unhandled
    Message=”Error getting SCA”
    Source=”ATSMS”
    StackTrace:
    at ATSMS.GSMModem.GetSCA()
    at ATSMS.GSMModem.get_SMSC()
    at ATSMS.GSMModem.DecodeHexPDU(String PDU)
    at ATSMS.GSMModem.serialDriver_DataReceived(Object sender, SerialDataReceivedEventArgs e)
    at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src, SerialDataReceivedEventArgs e)
    at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object state)
    at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)
    at System.Threading.ExecutionContext.runTryCode(Object userData)
    at System.Runtime.CompilerServices.
    RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup
    (TryCode code, CleanupCode backoutCode, Object userData)
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)

  2. thoughtworks | Sep 30, 2007 | Reply

    What is your phone model ? You need to make sure your phone support the AT+CNMI command. Try to use HypterTerminal to check first.

  3. Prashant | Oct 12, 2007 | Reply

    Hi,

    Is it possible to notify when Mobile phone is connected to PC

  4. admin | Oct 12, 2007 | Reply

    This library connects to the mobile phone through infrared, serial or bluetooth using a pre-configured COM port. You can check if the PC is connected to the mobile phone by using the IsConnected method.

  5. Prashant | Oct 16, 2007 | Reply

    Hi,

    Do we require any additional software like PC Suite provided with phone. could you give some information how we can configure COM port.

    Thanks..

  6. admin | Oct 17, 2007 | Reply

    Read the post “Use the .NET SMS Library to Retrieve Phone Settings”

  7. Prashant | Nov 21, 2007 | Reply

    can we send flash messages through .NET sms lib?

  8. sandhya | Dec 15, 2007 | Reply

    hi,

    im using ATSMS to send sms from an asp.net web page but it works fine only when 1 user is sending sms. when 2 or more users are accesing the page, they can connect because i am using virtual ports but the sms is not sent.

    if u can please help solving this problem

    thanks

  9. shirou | Dec 30, 2007 | Reply

    hi…
    Hi! thank you for providing this SMS Library, it helps me developing SMS gateway for my project…
    i wnat to ask !! How it is possible to track down whether SMS was delivered or not.. thanks before

  10. Alwin | Feb 2, 2008 | Reply

    hey thoughworks,

    can you provide an example on how to catch the delivery reports. You do have an option to enable delivery reports, and the delivery report is indeed received by the phone (check on the phone), but the report isn’t catched by the NewMessageReceived event. Please help…

    thanks!

    Alwin

  11. Anil | Feb 7, 2008 | Reply

    hi… Can i connect the same server to my web page for sending sms

  12. sa | May 15, 2008 | Reply

    I am getting garbage when i received a Message. I am using Motrolla L7 Mobile. Pls help.

  13. Isam | May 22, 2008 | Reply

    i test it with noki 6220 old model it work

  14. Teddy | Jun 15, 2008 | Reply

    Hi, I’m using your library for a project, can you tell me how to check the messages in the SIM memory or phone memory using your library

  15. Dewo | Jun 15, 2008 | Reply

    I have similar problem with Teddy, can you give an example on reading messages in the memory? I tried using GsmModem.MessageStore.Message(index).text but it failed, saying index out of bounds no matter what value i give to index.

  16. Robbie | Jun 28, 2008 | Reply

    love the work you’ve been doing but i am also having a problem with oGsmModem_NewMessageReceived sub i can only get the incoming call one to work and if you could help with listing and reading th emessages on the phone that would be great too thanks

  17. Mostafa | Aug 4, 2008 | Reply

    Hi .
    I Have a Nokia7610 .
    I will to Recive SMS or Read All My Inbox Phone , But Can’t .

    if You see NoKia PcSuite ,
    I wish Read Inbox such as PcSuite .
    Please take a Sample by (VB-C#).Net or Delphi …. for me .

    Please Help Me….

    Tanke You .
    Good Luck!

  18. Abed | Aug 14, 2008 | Reply

    how do I get the ATSMS source code?
    thanks.

  19. Abed | Aug 14, 2008 | Reply

    Hi, need your advice.

    how do we get the trigger from the modem / phone when the newmessage received.?

  20. shirou | Aug 15, 2008 | Reply

    to Abed
    i’m using timer to read message from phone every 20’s or less

  21. tapic | Oct 25, 2008 | Reply

    Hi,

    I have observed that multipart SMS messages (long messages) are not correctly handled. Is there a fix for this?

    I am fan of this library, I would be glad if you could solve this. Thx.

  22. Vivek | Dec 10, 2008 | Reply

    Yes, I too tried experimentin MULTIPART messages.
    If someone has the solution, please post it meanwhile I’ll try experimenting.

    Regards,
    Vivek.

  23. waleed | Feb 18, 2009 | Reply

    hi
    i wont to do program to send and resive message by sms server .
    i don’t know from what i well begin
    i need to help my
    by

  24. alejandromx | Feb 18, 2009 | Reply

    Hi, your library is great, congratulations, i having a problem when i send o receive multipart sms messages i think than the pdu encoding or decoding is not working when the message is received and when i send my sms in my headset i receive garbage, probably is the pdu encoding or is the UDH header that is not present 🙂 ….

    this is a good link explaining this
    http://www.activexperts.com/xmstoolkit/sms/multipart/

    Best Regards…

  25. Muhammad Adeel Aqdus | May 6, 2009 | Reply

    Hello I want to record a missed call records in my computer. I am trying to write this code but now succeeded yet can u help me out ?

  26. Ali Nase | Jun 1, 2009 | Reply

    could you please send the source code in C# or VC++?

  27. cinta | Jul 8, 2009 | Reply

    hye, im new here..can u plz help me on what device i should use when build a system besed on WEB in SMS technology..kindly help me..
    tankzz..

  28. shirou | Jul 9, 2009 | Reply

    to cinta!are you indonesian? U can use any device that can be modem on ur pc

  29. Muhammad Adeel Aqdus | Jul 14, 2009 | Reply

    Hello i want to retreive the mobile messages one by one with their sender cell number what is the code? please tell me?

    Thanks Take Care
    ALLAH HAFIZ

  30. admin | Jul 17, 2009 | Reply

    Hi All,

    There will be a new release of the library in C# in August which can be use to send long SMS, vCard, vCalendar, WAP push, ringtone, and picture message.

    The existing library is very much outdated since the last time I modified it.

  31. Ian | Aug 10, 2009 | Reply

    Hi,

    Really useful little app. I’d love to look at the ATSMS.DLL source to devleop our own.

    I particularly would like to change the ‘from’ number to our company name.

    eg instead of

    from : +44 07890123123

    to

    from : OurCompanySMSAlert

    would you please email me a copy when you’ve completed your updates this month.

    (I’ll subscribe to this post, so will get informed when a reply is posted!)

    Much appreciated.

    Ian

  32. R.S. Rajaram | Aug 15, 2009 | Reply

    Please provide me an example for catch the delivery reports. You do have an option to enable delivery reports, and the delivery report is indeed received by the phone (check on the phone), but the report isn’t catched by the NewMessageReceived event. Please help…

  33. Yasir Akram | Sep 19, 2009 | Reply

    Hello,
    The source code is very good, but the problem is i am unable to get the new message indication working, i am using SAMBA 75 GSM modem and when i click check phone button it displays the 2,1,0,0,1 as CNMI paramters. Please help!
    Regards

  34. Prashant | Oct 24, 2009 | Reply

    Hello
    Great Code,Congrats

    love the work you’ve been doing but i am also having a problem with oGsmModem_NewMessageReceived.
    Please Help me out of it,Here NewMessageReceived event not raised,in my case.
    Please Tell me Solution.
    How to know that my phone supports “+CNMI”
    My Mobile is LG GB230.

    waiting For Reply
    [email protected]

  35. Sneha | Jan 1, 2010 | Reply

    how to send Picture messages from pc using GSM modem with C# code.

  36. Yu Yang | Feb 23, 2010 | Reply

    I’m i also having problem with raising the
    oGsmModem_NewMessageReceived event.
    Please guide me on how to raise this event to enable receive messages. thanks!

1 Trackback(s)

  1. From how to use a laptop | Jan 13, 2010

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