使用 VB.NET 发送短信 [英] Sending SMS using VB.NET

查看:90
本文介绍了使用 VB.NET 发送短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 应用程序向手机发送短信.

I have written the above code in order to send SMS from my vb.net application to a Mobile phone.

当我执行这段代码时,我没有收到任何错误,同时我也没有收到任何短信.

When i execute this code i am not getting any errors, at the same time i am not receiving any SMS.

可能是什么问题?

推荐答案

我有一个完美的方式在visual basic中发送短信.

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

使用 AT 命令.

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

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

发送消息

首先:

把这段代码写在最上面

Imports System.IO.Ports
Imports System.IO

其次:

在表单的公共类中编写这段代码:

Write this code in the public class of form:

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

第三:

创建一个 textBox(TextmsgTextBox) 来编写文本消息,创建一个 TextBox2(MobileNumberTextBox) 来输入手机号码,以及一个 Button(SendBUT) 来发送消息.

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

这篇关于使用 VB.NET 发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆