Imports System.Net.Sockets
Imports System.Text

Public Class frmSendSMS
    Dim tcpClient As New System.Net.Sockets.TcpClient()
    Dim networkStream As NetworkStream
    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        Try
            'tcpClient = New System.Net.Sockets.TcpClient
            tcpClient.Connect(Me.txtServer.Text, Me.numPort.Value)
            networkStream = tcpClient.GetStream()
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte

            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            ' Output the data received from the host to the console.
            Dim returndata As String = Encoding.ASCII.GetString(bytes)

            SendData("HELO sms.netsmsgw.hyber.dk")
            Threading.Thread.Sleep(50)
            SendData("MAIL FROM:<" & Me.txtFrom.Text & ">")
            Threading.Thread.Sleep(50)
            SendData("RCPT TO:<" & Me.txtTo.Text & ">")
            Threading.Thread.Sleep(50)
            SendData("DATA")
            Threading.Thread.Sleep(50)
            SendData("Subject: " & Me.txtMsg.Text & vbCrLf & vbCrLf & "." & vbCrLf)
            Threading.Thread.Sleep(50)
            SendData("QUIT")

            MsgBox("Message was sent", MsgBoxStyle.Information, "Message Sent")
            Me.Close()

        Catch ex As Exception
            MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error")
        End Try
    End Sub


    Private Sub SendData(ByVal pstr As String)
        Try

            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(pstr)
            networkStream.Write(sendBytes, 0, sendBytes.Length)

            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.ReadTimeout = 1000
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            ' Output the data received from the host to the console.
            Dim returndata As String = Encoding.ASCII.GetString(bytes)
            'MsgBox(returndata)
        Catch ex As Exception
            MsgBox(ex.Message.ToString, MsgBoxStyle.Critical, "Error")
        End Try

    End Sub

End Class