Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
dear sir,
please help me i am makeing a desktop base application . in which i want to send sms to any mobile through internet service , and i have no idea how to code it.plz help me
thanks in advance.
Posted
Updated 7-Dec-17 21:26pm
Comments
[no name] 21-Apr-13 7:58am    
Ask your provider. They should be providing you sample code on using their service.

Hello, I use this library made by me, I hope this can help you, anyway if you have any problema or doubt about the code you can report to me,

So this is the code:

Imports System.IO.Ports
Imports System.IO.Ports.SerialPort
Imports System.Threading

Public Class smsclass


    Private WithEvents Serie As New SerialPort
    Private ReadThread As Thread

#Region "PROPIEDADES"

    Private CONTSMS As Boolean      '// Receives ">" that means that you have to write the body of the SMS
    Private ENDSMS As Boolean       '// Receive "+CMGS" that means that the SMS has been succesfully sent
    Private ERRORSMS As Boolean     '// It becomes true when an error occurred

#End Region

#Region "EVENTS"

    Public Event DataReceived(ByVal Message As String)  'Data received from the Serial port
    Public Event SMSSended(ByVal NumTelefono As String, ByVal Content As String, ByVal Id As String) 'SMS has been succesfully sent
    Public Event SMSError(ByVal Message As String)  'There was an error sending the SMS

#End Region

#Region "PROPERTIES"

    'Check the Serial Port Status

    Public ReadOnly Property IsOpen() As Boolean
        Get
            If Serie.IsOpen = True Then
                IsOpen = True
            Else
                IsOpen = False
            End If
        End Get
    End Property
#End Region


#Region "PUBLIC METHODS"

    'Public Method for Open the serial Port

    Public Sub Open(ByVal PortName As String, ByVal Speed As String)

        If IsOpen = False Then

            'Serial Port configuration
            Serie.PortName = PortName
            Serie.BaudRate = Speed
            Serie.DataBits = 8
            Serie.Parity = IO.Ports.Parity.None
            Serie.Handshake = IO.Ports.Handshake.RequestToSend

            Serie.RtsEnable = True
            Serie.DtrEnable = True

            ReadThread = New Thread(AddressOf Read)

            'Open the Serial Port and Start thread to read the port serial incoming data

            Try
                Serie.Open()
                ReadThread.Start()
            Catch ex As Exception

            End Try

        Else

        End If

    End Sub

    'Method to close serial port

    Public Sub Close()

        If IsOpen = True Then
            Serie.Close()
        End If
        ReadThread.Abort()
    End Sub

    'Method to Send a SMS

    Public Sub MandarSMS(ByVal ThelephoneNumber As String, ByVal Content As String, ByVal Id As String)

        '// States of the SMS progres to the initial value
        CONTSMS = False
        ENDSMS = False
        ERRORSMS = False

        If IsOpen = True Then
            Serie.WriteLine("AT+CMGS=" & ThelephoneNumber & vbCr)    '// Destinatario

            While CONTSMS = False
                System.Threading.Thread.Sleep(30)
            End While

            If (CONTSMS = True) Then

                Serie.WriteLine(Content & vbCrLf & Chr(26))  '// SMS CONTENT

            End If

            While (ENDSMS = False)
                System.Threading.Thread.Sleep(30)

                'THE MESSAGE COULD NOT BE DELIVERED
                If ERRORSMS = True Then

                    RaiseEvent SMSError(ThelephoneNumber) 'SMS ERROR
                    Exit While

                End If
            End While

            If ENDSMS = True Then
                RaiseEvent SMSSended(ThelephoneNumber, Content, Id) 'SMS WAS SENT
            End If

            ' Restart SMS status vars
            ENDSMS = False
            CONTSMS = False
            ERRORSMS = False

        End If

        System.Threading.Thread.Sleep(1000)

    End Sub

#End Region


#Region "PRIVATE METHODS"
    'Leer Puerto Serie

    Private Sub Read()
        Dim Incoming As String = Nothing
        While Serie.IsOpen = True
            If (Serie.BytesToRead <> 0) And (IsOpen = True) Then
                While Serie.BytesToRead <> 0

                    System.Threading.Thread.Sleep(30)

                    Incoming = Serie.ReadExisting()

                    If Incoming.Contains(">") Then
                        CONTSMS = True
                    End If

                    If Incoming.Contains("+CMGS:") Then
                        ENDSMS = True
                    End If

                    If Incoming.Contains("ERROR") Then
                        ERRORSMS = True
                    End If

                    RaiseEvent DataReceived(Incoming)
                End While

            End If

            System.Threading.Thread.Sleep(300)
        End While
    End Sub
#End Region


End Class
 
Share this answer
 
Comments
darabbitdj 14-Nov-23 12:59pm    
Hi Daiiniel ,
can i contact you at your mail address ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900