Sending SMS using VB.NET

こ雲淡風輕ζ 提交于 2019-12-03 21:32:39

I have a perfect way to send SMS in visual basic.

Using AT-commands.

AT-commands:are instructed through which you can send and receive SMS messages, and this is an example:

To Send a message

First:

Write this code in the top

Imports System.IO.Ports
Imports System.IO

Secondly:

Write this code in the public class of form:

Dim SerialPort As New System.IO.Ports.SerialPort()
Dim CR As String

Thirdly:

Create a textBox(TextmsgTextBox) to write the text message, and TextBox2(MobileNumberTextBox) to enter the mobile number, and Button(SendBUT) to Send message.

And write this code in the button click event.

If SerialPort.IsOpen Then
    SerialPort.Close()
End If
SerialPort.PortName = COM4
SerialPort.BaudRate = 9600
SerialPort.Parity = Parity.None
SerialPort.StopBits = StopBits.One
SerialPort.DataBits = 8
SerialPort.Handshake = Handshake.RequestToSend
SerialPort.DtrEnable = True
SerialPort.RtsEnable = True
SerialPort.NewLine = vbCrLf

Dim message As String
message = MsgRichTextBox.Text

Try
    SerialPort.Open()
Catch ex As Exception
    MsgBox("The modem with the port '" & SerialPort.PortName & "'is not plugged in!!" & vbcrlf & "Please plug the modem and try again.")
End Try

If SerialPort.IsOpen() Then
    SerialPort.Write("AT" & vbCrLf)
    SerialPort.Write("AT+CMGF=1" & vbCrLf)
    SerialPort.Write("AT+CMGS=" & Chr(34) & phoneNumBox.Text & Chr(34) & vbCrLf)
    SerialPort.Write(message & Chr(26))
    SentPicture.Visible = True
    SentLabel.Visible = True
    SentTimer.Start()
Else
    MsgBox("Port '" & SerialPort.PortName & "' is not available!")
End If
Pembuatan Program

Simple Send SMS using VB.NET + AT Command :


        Try
            With SerialPort1
                .Write("at+cmgf=1" & vbCrLf)
                Threading.Thread.Sleep(1000)
                .Write("at+cmgs=" & Chr(34) & TextBox1.Text & Chr(34) & vbCrLf)
                .Write(TextBox2.Text & Chr(26))
                Threading.Thread.Sleep(1000)
            End With
        Catch ex As Exception

        End Try

Port name change from time to another and from computer to another.

I will show you the way by pictures.

1:Enter to Device Manager from Control Panel.

2:Right click on the device, and choose Properties.

3:Choose Modem tap, and look for port name, and use it in your application.

user3706834
 Dim dt As New DataTable
        CreateDataTable(dt, "select * from Table where Id = 1")
        If (dt.Rows.Count > 0) Then
            Dim request As HttpWebRequest
            Dim response As HttpWebResponse = Nothing
            Dim url As String
            Dim senderid As String = dt.Rows(0).Item("SenderId").ToString()
            Dim password As String = dt.Rows(0).Item("Password").ToString()
            Dim host As String
            Dim originator As String = dt.Rows(0).Item("UserName").ToString()
            Try
                host = "http://smsidea.co.in/sendsms.aspx?"
                'originator = "3423434343"
                'password = "234hj"
                url = host + "mobile=" & HttpUtility.UrlEncode(originator) _
                         & "&pass=" + HttpUtility.UrlEncode(password) _
                         & "&senderid=" + HttpUtility.UrlEncode(senderid) _
                         & "&to=" + HttpUtility.UrlEncode(StrToNumber) _
                         & "&msg=" + HttpUtility.UrlEncode(StrBody)
                request = DirectCast(WebRequest.Create(url), HttpWebRequest)
                response = DirectCast(request.GetResponse(), HttpWebResponse)
                'MessageBox.Show("Response: " & response.StatusDescription)
            Catch ex As Exception
            End Try
        End If

Vb.net code for send sms.

Try

        Dim url As String

        'paste your sms api code to url

        'url = "http://xxxxxxxxxx.com/SMS_API/sendsms.php?username=XXXX&password=XXXXX&mobile=" + mobile + "&sendername=XXXX&message=XXXXX&routetype=1"


        url="Paste your api code"



        Dim myReq As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        Dim myResp As HttpWebResponse = DirectCast(myReq.GetResponse(), HttpWebResponse)
        Dim respStreamReader As New System.IO.StreamReader(myResp.GetResponseStream())
        Dim responseString As String = respStreamReader.ReadToEnd()
        respStreamReader.Close()
        myResp.Close()
        MsgBox("ok")

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

http://yii2ideas.blogspot.in/2017/11/how-to-send-sms-from-vb-net-application.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!