Windows Forms: Send SMS with 3G Modem in C#

How to Send SMS Message via GSM Modem, 3G Modem, using AT Commands in C#.

AT commands are instructions used to control a modem. Every command line starts with "AT" or "at"

Step 1Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "SendSMS" and then click OK

send sms in c#Step 2: Design your send sms form as below

send sms in c#

Step 3: Add a click event handler to the button

private void btnSend_Click(object sender, EventArgs e)
{
    try
    {
        //Using serial port to send sms message
        SerialPort sp = new SerialPort();
        sp.PortName = txtPort.Text;
        sp.Open();
        //Using AT command to send sms
        sp.WriteLine("AT" + Environment.NewLine);
        Thread.Sleep(100);
        sp.WriteLine("AT+CMGF=1" + Environment.NewLine);
        Thread.Sleep(100);
        sp.WriteLine("AT+CSCS=\"GSM\"" + Environment.NewLine);
        Thread.Sleep(100);
        sp.WriteLine("AT+CMGS=\"" + txtPhoneNumber.Text + "\"" + Environment.NewLine);//Set phone number
        Thread.Sleep(100);
        sp.WriteLine(txtMessage.Text + Environment.NewLine);//Set messages
        Thread.Sleep(100);
        sp.Write(new byte[] { 26 }, 0, 1);
        Thread.Sleep(100);
        var response = sp.ReadExisting();
        if (response.Contains("ERROR"))
            MessageBox.Show("Send failed !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        else
            MessageBox.Show("SMS Sent !", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        sp.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

After successful connection of the GSM/GPRS modem with your PC, you can send sms message from this application

VIDEO TUTORIALS