Visual Basic 8 (2005)Visual C++ 8.0Windows VistaWindows 2003WebFormsVisual Studio 2005Windows 2000Windows XP.NET 2.0IntermediateDevVisual StudioWindowsC++.NETVisual BasicASP.NETC#
使用 .NET 发送 SMS






3.81/5 (53投票s)
本文介绍了一些使用 .NET 发送 SMS 的方法。
引言
您希望何时通过应用程序发送 SMS?可能有大量的使用场景。最简单的一种是验证手机号码,而一些复杂的情况可能涉及在庞大的工作流程完成或失败后发送 SMS。让我们找出使用 C#/VB.NET 发送 SMS 的方法。
发送短信
可以通过哪些方式发送 SMS?
- 使用 GSM 调制解调器
- 适用于需要实现离线应用程序,并且每分钟发送少量 SMS(通常几十条)的情况。
- 使用 Web 服务
- 适用于在线应用程序,并且每分钟发送少量 SMS(通常几十条)的情况。
- 使用服务提供商提供的端点
- 适用于每分钟发送的 SMS 数量超过几百条的情况。服务提供商要求每月至少发送 100,000 条 SMS 的承诺。
通过 Web 服务或端点发送 SMS 是最简单的。相比之下,通过 GSM 调制解调器发送 SMS 需要一些额外的步骤来处理。让我们详细了解每种方式。
通过 GSM 调制解调器发送 SMS
- 首先,找到最适合您需求的 GSM 调制解调器。规格可在此处找到。
- 了解与调制解调器通信所需的AT 命令集。
- 根据 GSM 调制解调器附带手册中指定的设置指南,将调制解调器连接到计算机。Maestro 20/100 调制解调器的连接示例详情可在此处找到。此处解释的连接设置对于大多数 GSM 调制解调器都是通用的。
- 创建一个新的 Windows 应用程序或 Web 应用程序。
- 添加一个名为 SMSCOMMS 的新类文件。
- 将下面的代码复制并粘贴到类中。
使用 VB.NET 编码
如果您使用 VB.NET 为应用程序编码,可以使用Jeanred共享的类。详情如下:
Option Explicit On
Imports System
Imports System.Threading
Imports System.ComponentModel
Imports System.IO.PortsPublic Class SMSCOMMS
Private WithEvents SMSPort As SerialPort
Private SMSThread As Thread
Private ReadThread As Thread
Shared _Continue As Boolean = False
Shared _ContSMS As Boolean = False
Private _Wait As Boolean = False
Shared _ReadPort As Boolean = False
Public Event Sending(ByVal Done As Boolean)
Public Event DataReceived(ByVal Message As String)
Public Sub New(ByRef COMMPORT As String)
SMSPort = New SerialPort
With SMSPort
.PortName = COMMPORT
.BaudRate = 9600
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
.Handshake = Handshake.RequestToSend
.DtrEnable = True
.RtsEnable = True
.NewLine = vbCrLf
End With
ReadThread = New Thread(AddressOf ReadPort)
End Sub
Public Function SendSMS(ByVal CellNumber As String,
ByVal SMSMessage As String) As Boolean
Dim MyMessage As String = Nothing
'Check if Message Length <= 160
If SMSMessage.Length <= 160 Then
MyMessage = SMSMessage
Else
MyMessage = Mid(SMSMessage, 1, 160)
End If
If IsOpen = True Then
SMSPort.WriteLine("AT+CMGS=" & CellNumber & vbCr)
_ContSMS = False
SMSPort.WriteLine(MyMessage & vbCrLf & Chr(26))
_Continue = False
RaiseEvent Sending(False)
End If
End Function
Private Sub ReadPort()
Dim SerialIn As String = Nothing
Dim RXBuffer(SMSPort.ReadBufferSize) As Byte
Dim SMSMessage As String = Nothing
Dim Strpos As Integer = 0
Dim TmpStr As String = Nothing
While SMSPort.IsOpen = True
If (SMSPort.BytesToRead <> 0) And (
SMSPort.IsOpen = True) Then
While SMSPort.BytesToRead <> 0
SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize)
SerialIn =
SerialIn & System.Text.Encoding.ASCII.GetString(
RXBuffer)
If SerialIn.Contains(">") = True Then
_ContSMS = True
End If
If SerialIn.Contains("+CMGS:") = True Then
_Continue = True
RaiseEvent Sending(True)
_Wait = False
SerialIn = String.Empty
ReDim RXBuffer(SMSPort.ReadBufferSize)
End If
End While
RaiseEvent DataReceived(SerialIn)
SerialIn = String.Empty
ReDim RXBuffer(SMSPort.ReadBufferSize)
End If
End While
End Sub
Public ReadOnly Property IsOpen() As Boolean
Get
If SMSPort.IsOpen = True Then
IsOpen = True
Else
IsOpen = False
End If
End Get
End Property
Public Sub Open()
If IsOpen = False Then
SMSPort.Open()
ReadThread.Start()
End If
End Sub
Public Sub Close()
If IsOpen = True Then
SMSPort.Close()
End If
End Sub
End Class
上述类公开了三个函数:Open
、SendSMS
和 Close
。创建类的实例时,提供调制解调器连接到的端口。在 Windows 应用程序中,请按照以下步骤操作:
SMSEngine = New SMSCOMMS("COM1")
SMSEngine.Open()
SMSEngine.SendSMS("919888888888","SMS Testing")
SMSEngine.Close()
使用 C# 编码
该代码的 C# 实现如下:
using System;
using System.Threading;
using System.ComponentModel;
using System.IO.Ports;
public class SMSCOMMS
{
private SerialPort SMSPort;
private Thread SMSThread;
private Thread ReadThread;
public static bool _Continue = false;
public static bool _ContSMS = false;
private bool _Wait = false;
public static bool _ReadPort = false;
public delegate void SendingEventHandler(bool Done);
public event SendingEventHandler Sending;
public delegate void DataReceivedEventHandler(string Message);
public event DataReceivedEventHandler DataReceived;
public SMSCOMMS(ref string COMMPORT)
{
SMSPort = new SerialPort();
SMSPort.PortName = COMMPORT;
SMSPort.BaudRate = 9600;
SMSPort.Parity = Parity.None;
SMSPort.DataBits = 8;
SMSPort.StopBits = StopBits.One;
SMSPort.Handshake = Handshake.RequestToSend;
SMSPort.DtrEnable = true;
SMSPort.RtsEnable = true;
SMSPort.NewLine = System.Environment.NewLine;
ReadThread = new Thread(
new System.Threading.ThreadStart(ReadPort));
}
public bool SendSMS(string CellNumber, string SMSMessage)
{
string MyMessage = null;
//Check if Message Length <= 160
if (SMSMessage.Length <= 160)
MyMessage = SMSMessage;
else
MyMessage = SMSMessage.Substring(0, 160);
if (IsOpen == true)
{
SMSPort.WriteLine("AT+CMGS=" + CellNumber + "r");
_ContSMS = false;
SMSPort.WriteLine(
MyMessage + System.Environment.NewLine + (char)(26));
_Continue = false;
if (Sending != null)
Sending(false);
}
return false;
}
private void ReadPort()
{
string SerialIn = null;
byte[] RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
string SMSMessage = null;
int Strpos = 0;
string TmpStr = null;
while (SMSPort.IsOpen == true)
{
if ((SMSPort.BytesToRead != 0) & (SMSPort.IsOpen == true))
{
while (SMSPort.BytesToRead != 0)
{
SMSPort.Read(RXBuffer, 0, SMSPort.ReadBufferSize);
SerialIn =
SerialIn + System.Text.Encoding.ASCII.GetString(
RXBuffer);
if (SerialIn.Contains(">") == true)
{
_ContSMS = true;
}
if (SerialIn.Contains("+CMGS:") == true)
{
_Continue = true;
if (Sending != null)
Sending(true);
_Wait = false;
SerialIn = string.Empty;
RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
}
}
if (DataReceived != null)
DataReceived(SerialIn);
SerialIn = string.Empty;
RXBuffer = new byte[SMSPort.ReadBufferSize + 1];
}
}
}
public bool SendSMS(string CellNumber, string SMSMessage)
{
string MyMessage = null;
if (SMSMessage.Length <= 160)
{
MyMessage = SMSMessage;
}
else
{
MyMessage = SMSMessage.Substring(0, 160);
}
if (IsOpen == true)
{
SMSPort.WriteLine("AT+CMGS=" + CellNumber + "r");
_ContSMS = false;
SMSPort.WriteLine(
MyMessage + System.Environment.NewLine + (char)(26));
_Continue = false;
if (Sending != null)
Sending(false);
}
return false;
}
public void Open()
{
if (IsOpen == false)
{
SMSPort.Open();
ReadThread.Start();
}
}
public void Close()
{
if (IsOpen == true)
{
SMSPort.Close();
}
}
}
然后像这样使用代码:
SMSEngine = new SMSCOMMS("COM1");
SMSEngine.Open();
SMSEngine.SendSMS("919888888888","THIS IS YOUR MESSAGE");
SMSEngine.Close();
通过 Web 服务发送 SMS
通过 Web 服务发送 SMS,虽然不适合实时服务,但却是一种成本效益很高的方法。有很多 Web 服务,您可以通过搜索网络找到一个。有些免费的服务不太可靠。因此,购买 SMS 积分以使用 Web 服务发送有限数量的 SMS。在这里,使用非常简单,因为它只是调用一个 Web 服务将号码和消息传递给一个函数。Code Project 示例可在Code Project上找到。
通过服务提供商端点发送 SMS
通过服务提供商发送 SMS 也与使用 Web 服务类似。这里可能是一种非标准协议或通过 HTTP。这取决于服务提供商。有些提供可以用于编程自定义应用程序的示例代码。
历史
- 2007 年 6 月 1 日 - 发布原始版本。
- 2007 年 6 月 11 日 - 文章已编辑并发布到 CodeProject.com 的主要文章库。