- This topic explains about, how to create sms application using C# programming or we can send sms through PC using C# programming.
- For create sms application, two classes have been used, Form1 and SMSCOMMS
- You have to import following namespaces for creating sms application using C#.
|
1 2 3 |
using System.Threading; using System.ComponentModel; using System.IO.Ports; |
System.ComponentModel - Create sms application or send sms using C#
- The System.ComponentModel namespace provides classes that are used to implement the run-time and design-time behavior of components and controls.
- This namespace includes the base classes and interfaces for implementing attributes and type converters, binding to data sources, and licensing components.
System.IO.Ports - Create sms application or send sms using C#
- The System.IO.Ports namespace contains classes for controlling serial ports.
- The most important class, SerialPort, provides a framework for synchronous and event-driven I/O, access to pin and break states, and access to serial driver properties.
- It can be used to wrap a Stream objects, allowing the serial port to be accessed by classes that use streams.
Create sms application or send sms using C# – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Threading; using System.ComponentModel; using System.IO.Ports; namespace WindowsFormsApplication1 { public partial class Form1 : Form { //connect your mobile/GSM modem to PC, //then go to device manager and check under ports which COM port has been selected //if it say COM4, then put COM5 in following statement SMSCOMMS SMSEngine = new SMSCOMMS("COM5"); int i; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { SMSEngine.Open(); // open the port SMSEngine.SendSMS(); // send the SMS } } public class SMSCOMMS { private SerialPort SMSPort; private Thread SMSThread; private Thread ReadThread; static bool _Continue = false; static bool _ContSMS = false; private bool _Wait = false; static bool _ReadPort = false; public event SendingEventHandler Sending; public delegate void SendingEventHandler(bool Done); public event DataReceivedEventHandler DataReceived; public delegate void DataReceivedEventHandler(string Message); public SMSCOMMS(ref string COMMPORT) { //initialize all values SMSPort = new SerialPort(); var _with1 = SMSPort; _with1.PortName = COMMPORT; _with1.BaudRate = 9600; _with1.Parity = Parity.None; _with1.DataBits = 8; _with1.StopBits = StopBits.One; _with1.Handshake = Handshake.RequestToSendXOnXOff; _with1.DtrEnable = true; _with1.RtsEnable = true; _with1.NewLine = Constants.vbCrLf; } public bool SendSMS() { try { if (SMSPort.IsOpen == true) { //sending AT commands SMSPort.WriteLine("AT"); SMSPort.WriteLine("AT+CMGF=1" + Constants.vbCrLf); //set command message format to text mode(1) SMSPort.WriteLine("AT+CSCA=" + Strings.Chr(34) + "+919895051914" + Strings.Chr(34) + Constants.vbCrLf); //set service center number (which varies for service providers (idea, airtel)) SMSPort.WriteLine("AT+CMGS=" + Strings.Chr(34) + "your receipent mobile no" + Strings.Chr(34) + Constants.vbCrLf); // enter the mobile number whom you want to send the SMS _ContSMS = false; SMSPort.WriteLine("Hello, SMS sending success " + Constants.vbCrLf + Strings.Chr(26)); //SMS sending //SMSPort.ReadLine() Interaction.MsgBox(SMSPort.ReadExisting()); MessageBox.Show("Message sent"); SMSPort.Close(); } } catch (Exception ex) { MessageBox.Show (ex.Message); } } public void Open() { if (!(SMSPort.IsOpen == true)) { SMSPort.Open(); } } public void Close() { if (SMSPort.IsOpen == true) { SMSPort.Close(); } } } } |
Create sms application or send sms using C#






