用MTS调制解调器在C#.net Windows应用程序中发送SMS到移动的

jpfvwuh4  于 2022-12-19  发布在  Windows
关注(0)|答案(3)|浏览(159)

这是消息框显示的代码(“消息发送成功”)。但是我没有收到消息到我使用的手机。

SerialPort sp = new SerialPort();
sp.PortName = "COM4";//choose your port wisely
sp.BaudRate = 9600;
sp.Parity = Parity.None;
sp.Open();
sp.Write("AT+CMGS=\";+91" + textBox1.Text + "\"" + Environment.NewLine);
Thread.Sleep(2000);
sp.Write(textBox2.Text + (char)26 + Environment.NewLine);
MessageBox.Show("Message sent successfully");
esyap4oy

esyap4oy1#

这是我的代码,它为我工作100%:

private SerialPort _serialPort;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        string number = textBox1.Text;
        string message = richTextBox1.Text;


        //Replace "COM8"withcorresponding port name
        _serialPort = new SerialPort("COM8", 115200);

        Thread.Sleep(100);

        _serialPort.Open();

        Thread.Sleep(100);

        _serialPort.Write("AT+CMGF=1\r");

        Thread.Sleep(100);

        _serialPort.Write("AT+CMGS=\"" + number + "\"\r\n");

        Thread.Sleep(100);

        _serialPort.Write(message + "\x1A");

        Thread.Sleep(300);

        label1.Text = "Message sent !!";

        _serialPort.Close();
    }
sbdsn5lh

sbdsn5lh2#

请输入验证码:

private void Send()
{
    SerialPort sp = new SerialPort();
    sp.DataReceived += new SerialDataReceivedEventHandler(OnDataReceived);
    sp.PortName = "COM4";//choose your port wisely
    sp.BaudRate = 9600;
    sp.Parity = Parity.None;
    sp.Open();

    // Set the GSM modem to Text Mode
    sp.WriteLine("AT+CMGF=1"+Environment.NewLine);
    // Specifying mobile number
    sp.WriteLine(string.Format("AT+CMGS=\"+91{0}\"{1}", textBox1.Text, Environment.NewLine));
    // Specifying sms body
    sp.WriteLine(textBox2.Text + (char)26 + Environment.NewLine);
    MessageBox.Show("Message sent successfully");
}

private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    string modemResult = sp.ReadExisting();
    this.yourTextBox.Text += modemResult;
}

希望能有所帮助

fgw7neuy

fgw7neuy3#

这个问题冒了出来,所以我想用一种与今天高度相关的方法来回答可能会很好。正如法尔赞在对他的回答的评论中提到的那样,有一些服务提供商可以提供API,允许你发送SMS消息。2这一点现在更加重要,因为找到固定电话变得有些罕见,而找到安装了调制解调器的计算机则更加罕见。Twilio是可用的提供商之一,从开发的Angular 来看,它使发送SMS变得微不足道。

// Twilio usings
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

const string accountSid = "your_account_sid"; // specific to your Twilio account
const string authToken = "your_auth_token"; // specific to your Twilion account

TwilioClient.Init(accountSid, authToken);

// Send a new outgoing SMS by POSTing to the Messages resource
MessageResource.Create(
  from: new PhoneNumber("555-867-5309"), // From number must be an SMS-enabled Twilio number
  to: new PhoneNumber(textBox1.Text),
  body: textBox2.Text);  // Message content

MessageBox.Show("Message sent successfully");

Twilio是一个订阅服务,但他们有一个“现收现付”的计划,目前每条消息的费用不到0.01美元。

相关问题