简体   繁体   English

使用 MTS 调制解调器在 C#.net windows 应用程序中向手机发送短信

[英]Sending sms to mobile phone in C#.net windows application with MTS modem

This is the code the message box showing ("message sent successfully").这是消息框显示的代码(“消息发送成功”)。 But I didn't get the message to my phone that I used.但是我使用的手机没有收到消息。

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");

this is my code and its worked for me 100% :这是我的代码,它 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();
    }

Please try this code:请试试这个代码:

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;
}

Hope it helps希望能帮助到你

This question bubbled up, so I thought it might be good to answer with an approach that is highly relevant today.这个问题冒了出来,所以我认为用今天高度相关的方法来回答可能会很好。 As Farzan mentioned in a comment on his answer, there are service providers available that expose APIs which allow you to send SMS messages.正如 Farzan 在对他的回答的评论中提到的那样,有一些服务提供商可以公开允许您发送 SMS 消息的 API。 This is even more relevant now as it has become somewhat rare to find landline telephones and even more rare to find a computer with a modem installed.这一点现在更加重要,因为找到固定电话变得有些罕见,找到安装了调制解调器的计算机变得更加罕见。 Twilio is one of the available providers and has made sending an SMS trivial from a development perspective. Twilio 是可用的提供商之一,从开发的角度来看,发送 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 is a subscription service, but they have a "pay as you go" plan that currently costs less than $.01 (US) per message. Twilio 是一项订阅服务,但他们有“现收现付”计划,目前每条消息的成本低于 0.01 美元(美国)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM