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

时间:2014-01-10 19:53:58

标签: c# windows

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

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

3 个答案:

答案 0 :(得分:0)

这是我的代码,它为我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();
    }

答案 1 :(得分:0)

这个问题冒出来了,所以我认为用一种今天高度相关的方法回答可能会很好。正如Farzan在对他的回答的评论中提到的那样,有些服务提供商可以公开允许您发送SMS消息的API。这一点现在更加重要,因为找到固定电话已经变得很少见,甚至更难以找到安装了调制解调器的电脑。 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是一项订阅服务,但他们有“按需付费”计划,目前每条消息的费用低于0.01美元(美国)。

答案 2 :(得分:-1)

请尝试以下代码:

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

希望有所帮助