'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");
Solution 1:[1]
this is my code and its worked for me 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();
}
Solution 2:[2]
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
Solution 3:[3]
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. 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 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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | nassimlouchani |
Solution 2 | |
Solution 3 | Kyle Burns |