2014-01-10 18 views
1

这是消息框显示的代码(“消息已成功发送”)。但是我没有收到我使用过的电话信息。使用MTS调制解调器在C#.net窗口应用程序中向手机发送短信

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 seccuessfully"); 
+2

顺便说一句,你拼写“Secressfully”,它是成功的。 – Hamoudy

回答

0

请试试这个代码:

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

希望它可以帮助

+0

亲爱的朋友Farzan,谢谢你的热心帮助。但它不起作用。显示消息的消息框,但没有将消息传递给我选择的电话号码。 – user3171896

+0

我们需要找出问题,首先让我们看看调制解调器的答案是什么!我已经更新了代码。请在您的表单上放置一个TextBox并将其Multiline属性设置为true,然后适当地更改代码,以便将传入的数据放入您的TextBox中。尝试再次发送邮件并查看文本框中显示的数据,如果它无助于解决问题,请将传入的数据复制到此处。 – Farzan

+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(); 
    } 
+0

为什么有那么多'Thread.Sleep(X);'在那里? – LenglBoy

+0

由于您使用AT&T代码与设备进行通信,因此每次操作之间应该有制动。 – nassimlouchani

0

这个问题冒泡,所以我认为用今天高度相关的方法来回答可能会很好。正如Farzan在回答他的评论时提到的那样,有些服务提供商可以提供允许您发送SMS消息的API。现在这更加重要,因为找到固定电话已经变得很少见了,而且更难找到安装了调制解调器的计算机。 Twilio是可用的提供商之一,并从发展的角度发送短信琐事。

// 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美元。

相关问题