VB.NET

We offer code samples to make it easy for you to plug in SMS functionality into your applications.

Download our SDK that contains sample projects in many languages or go straight to the language you need with our code samples.

Code Sample

Public Class Message
Dim postURL As String = “http://gateway.2sms.us/xml/xml.jsp”

Dim username As String
Dim password As StringSub Message()
End Sub

Sub Message(ByVal username As String, ByVal password As String)
    Me.username = username
    Me.password = password
End Sub

Function sendMessage(ByVal destination As String, ByVal messageText As String)
    Dim xmlDoc As String = formXml(Me.username, Me.password, destination, messageText)
    Return sendXml(xmlDoc)
End Function

Function sendMessage(ByVal username As String, ByVal password As String, ByVal destination As String, ByVal messageText As String) As String
    Dim xmlDoc As String = formXml(username, password, destination, messageText)
    Return sendXml(xmlDoc)
End Function
Function formXml(ByVal username As String, ByVal password As String, ByVal destination As String, ByVal messageText As String) As String
    ‘ create xml string
    Dim xmlString As String = “<?xml version=” & Chr(34) & “1.0” & Chr(34) & ” encoding=” & Chr(34) &
“UTF-8″ & Chr(34) & ” ?>” & _
        “<Request xmlns:xsi=” & Chr(34) & “http://www.w3.org/2001/XMLSchema-instance” & Chr(34) & ” xsi:noNamespaceSchemaLocation=” & Chr(34) & “http://schema.2sms.us/1.0/0410_RequestSendMessage.xsd” &
Chr(34) & ” Version=” & Chr(34) & “1.0” & Chr(34) & “>” & _
    “<Identification>” & _
        “<UserID>” & username & “</UserID>” & _
        “<Password>” & password & “</Password>” & _
    “</Identification>” & _
    “<Service>” & _
    “<ServiceName>SendMessage</ServiceName>” & _
    “<ServiceDetail>” & _
        “<SingleMessage>” & _
            “<Destination>” + destination + “</Destination>” & _
            “<Text>” + messageText + “</Text>” & _
        “</SingleMessage>” & _
    “</ServiceDetail>” & _
    “</Service>” & _
  “</Request>”

 Return xmlString
End Function

Function sendXml(ByVal xmlDoc As String) As String
    Dim resultText As String
    Dim request As HttpWebRequest
    request = CType(HttpWebRequest.Create(postURL), HttpWebRequest)
    request.Method = “POST”
    Dim writer As StreamWriter = New StreamWriter(request.GetRequestStream())
    writer.Write(xmlDoc)
    writer.Flush()
    writer.Close()
    Try
    Dim reader As XmlTextReader = New XmlTextReader(request.GetResponse().GetResponseStream())
    Do While (reader.Read())
    If (reader.NodeType = XmlNodeType.Element) Then
        If (reader.Name = “Error”) Then
        Dim errorCode As String
        Dim errorReason As String
        Do While (Not (reader.Name = “Error” And reader.NodeType = XmlNodeType.Element))
               reader.Read()
               If (reader.NodeType = XmlNodeType.Element) Then
                    If (reader.Name = “ErrorCode”) Then
                         reader.Read()
                         errorCode = reader.Value
                    ElseIf (reader.Name = “ErrorReason”) Then
                         reader.Read()
                         errorReason = reader.Value
                    End If
               End If
           Loop
           If (Not errorCode = “00”) Then
               resultText = reader.Value
           End If
           ElseIf (reader.Name = “Result”) Then
                  reader.Read()
                  resultText = reader.Value
           End If
      End If
      Loop
      Catch
       resultText = “An error occurred”
    End Try

Return resultText
End Function

Sub setUsername(ByVal username As String)

    Me.username = username
End Sub

Sub setPassword(ByVal password As String)

    Me.password = password
End Sub
End Class

Scroll to Top