How to send SMS message from Visual Basic

This guide provides you information on how to send SMS text messages from Visual Basic. Text messages will be sent from VB 6 through HTTP with HTTP GET method. To achieve this solution download and extract file below in which the whole project is available.

Download: vb6-example-for-excel-v1.1.xls
Download: vb6-example-for-access-v1.1.mdb

Download, extract and start the project.

On the appeared form provide the following values:

IP address: IP address of Ozeki NG SMS Gateway (
Port number: the port number of Ozeki NG SMS Gateway (9501)
Username: the username you use to log into Ozeki NG SMS Gateway (by default it is admin)
Password: the password used in Ozeki NG SMS Gateway (by default it is abc123)
Recipient: the phone number of the recipient (e.g. +36301122334)
Message: the body of the message (e.g. Test Message)

After filling the form click on Send Message (Figure 1).

provide the message values
Figure 1 - Provide values

On Figure 2 you can see the response from Ozeki NG SMS Gateway.

get a response from ozeki ng sms gateway
Figure 2 - Response from Ozeki NG SMS Gateway

Figure 3 demonstrates the sent message in Ozeki NG SMS Gateway.

watch your sent messages in ozeki ng sms gateway
Figure 3 - Sent message in Ozeki NG SMS Gateway

Source code

'This function opens an internet connection
Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" _
        (ByVal ozAgent As String, ByVal ozAccessType As Long, ByVal ozProxyName As String, _
        ByVal ozProxyBypass As String, ByVal ozFlags As Long) As Long

'This function handles the closing of the internet connection
        Private Declare Function InternetCloseHandle Lib "wininet" (ByRef ozInet As Long) As Long

'Reads the response file of the retrieved file requested by HTTP
Private Declare Function InternetReadFile Lib "wininet" _
        (ByVal ozGetFile As Long, ByVal ozBuffer As String, ByVal ozNumBytesToRead As Long,
        ozNumberOfBytesRead As Long) As Integer

'Opens an URL via the internet connection
Private Declare Function InternetOpenUrl Lib "wininet" Alias "InternetOpenUrlA" _
        (ByVal ozInternetSession As Long, ByVal ozUrl As String, ByVal ozHeaders As String, _
        ByVal ozHeadersLength As Long, ByVal ozFlags As Long, ByVal ozContext As Long) As Long


InternetOpen function creates Internet connection
InternetCloseHandle closes the connection
InternetOpenUrl opens an URL
InternetReadFile saves the HTTP response

'Sends the actual HTTP request

Private Function SendRequest(ByVal strUrl As String) As String
        Dim ozConnOpen As Long, ozGetFile As Long
        Dim ozReturnValue As Long, ozBuffer As String * 128
        Dim ozData As String

                ozConnOpen = InternetOpen("Ozeki HTTP client", 1, vbNullString, vbNullString, 0)

                If ozConnOpen = 0 Then
                        MsgBox "No Internet connection avaible"
                        Exit Function
                End If

        ozGetFile = InternetOpenUrl(ozConnOpen, strUrl, vbNullString, 0, &H4000000, 0)
                InternetReadFile ozGetFile, ozBuffer, 128, ozReturnValue
                ozData = ozBuffer
                        Do While ozReturnValue <> 0
                                InternetReadFile ozGetFile, ozBuffer, 128, ozReturnValue
                                ozData = ozData + Mid(ozBuffer, 1, ozReturnValue)

        InternetCloseHandle ozGetFile
        InternetCloseHandle ozConnOpen

        SendRequest = ozData
        ozData = ""
End Function

SendRequest function requests the URL

Private Sub Command1_Click()

    Dim ozIpaddr As String
    Dim ozPortNum As String
    Dim ozUser As String
    Dim ozPass As String
    Dim ozRecipient As String
    Dim ozMessage As String

        ozIpaddr = Text1.Text
        ozPortNum = Text2.Text
        ozUser = Text3.Text
        ozPass = Text4.Text
        ozRecipient = Text5.Text
        ozMessage = Text6.Text

Dim SendString As String

'SendString variable will contain the HTTP GET string, which will be used to send the message.

SendString = "http://" + ozIpaddr + ":" + ozPortNum _
    & "/api?action=sendmessage&username=" + ozUser _
    & "&password=" + ozPass _
    & "&recipient=" + ozURLEncode(ozRecipient) _
    & "&messagetype=SMS:TEXT&messagedata=" + ozURLEncode(ozMessage)

'Sending the SMS with the HTTP request, and writing the response xml to the frame

        Label7.Caption = SendRequest(SendString)

End Sub


Click event of Command1 button will execute the program and write the response XML


'This function will URL encode the characters in the HTTP request.

Function ozURLEncode(ByVal Text As String) As String
    Dim i As Integer
    Dim ozCode As Integer
    Dim char As String

    ozURLEncode = Text

    For i = Len(ozURLEncode) To 1 Step -1
        ozCode = Asc(Mid$(ozURLEncode, i, 1))
        Select Case ozCode
            Case 48 To 57, 65 To 90, 97 To 122
                ' Do not replace the alphanumeric characters
            Case 32
                ' Replace the space character with "+"
                Mid$(ozURLEncode, i, 1) = "+"
            Case Else
                ' Replace the national characters with a percent sign and the characters
                hexadecimal value
                ozURLEncode = Left$(ozURLEncode, i - 1) & "%" & Hex$(ozCode) & Mid$ _
                    (ozURLEncode, i + 1)
        End Select

End Function

ozURLEncode URL encodes its parameter

More information