使用VB.NET发送SMS

时间:2014-01-15 07:48:23

标签: vb.net sms vb.net-2010

Dim message As New MailMessage()
message.To.Add("9999999999@ideacellular.net")
message.From = New MailAddress("xyz@gmail.com")
message.Subject = "Hi"
message.Body = "SMS"
Dim smtp As New SmtpClient("smtp.gmail.com")
smtp.EnableSsl = True
smtp.Credentials = New System.Net.NetworkCredential("xyz@gmail.com", "password")
smtp.Send(message)

我已经编写了上面的代码,以便从我的vb.net应用程序向手机发送短信。

当我执行此代码时,我没有收到任何错误,同时我没有收到任何短信。

可能是什么问题?

5 个答案:

答案 0 :(得分:2)

我有一个完美的方式来发送基本的Visual Basic。

使用AT命令。

AT-commands:指示您通过它发送和接收SMS消息, 这是一个例子:

发送消息

首先:

将此代码写在顶部

Imports System.IO.Ports
Imports System.IO

其次:

将此代码写入表单的公共类:

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

第三

创建一个textBox(TextmsgTextBox)来写入文本消息,并使用TextBox2(MobileNumberTextBox)输入手机号码,并将Button(SendBUT)发送给发送消息。

并在按钮点击事件中编写此代码。

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

答案 1 :(得分:1)

使用VB.NET + AT命令简单发送短信:


        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

答案 2 :(得分:0)

端口名称随时间变化,从计算机变为另一个。

我会用图片向你展示。

1:从控制面板进入设备管理器。

enter image description here

2:右键单击设备,然后选择“属性”。 enter image description here

3:选择调制解调器点击,查找端口名称,并在您的应用程序中使用它。 enter image description here

答案 3 :(得分:0)

 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

答案 4 :(得分:0)

发送短信的Vb.net代码。

尝试

        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