Sending SMS with C sharp using mobile phone

Sending SMS Via Windows Form Application Using any Android Smart Phone 

Sending SMS with C sharp using mobile phone

Sending SMS with C sharp using mobile phone

Source Code 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GsmComm.GsmCommunication;
using GsmComm.Interfaces;
using GsmComm.PduConverter;
using GsmComm.Server;
using System.IO;
using System.Data.OleDb;
using System.Management;
namespace Helper
{
    public partial class Form1 : Form
    {

        public static Int16 Comm_Port = 0;
        public static Int32 Comm_BaudRate = 0;
        public static Int32 Comm_TimeOut = 0;
        public static GsmCommMain comm;
        DataSet dataSet, dataSet1 = new DataSet();
        
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {

            string CELL_Number, SMS_Message;

            SmsSubmitPdu pdu1;

            try
            {

                if (comm.IsConnected() == true)
                {
                    if (Cell_Num.Text.ToString() == "")
                    {
                        MessageBox.Show("Kindly Add Cell Number\r\n\nFor Example\n\n+9231234567", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    else if (SMS_Text.Text.ToString() == "")
                    {
                        MessageBox.Show("Kindly Add Your Message Text", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    CELL_Number = Cell_Num.Text.ToString();
                    SMS_Message = SMS_Text.Text.ToString();

                    MessageBox.Show("Kindly Re-Check Your SMS & CELL NUMBER \r\n\nCELL # : " + CELL_Number + "\r\n\nMESSAGE TEXT : "+SMS_Message ,"SMS INFO", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                    try
                    {
                        Cursor.Current = Cursors.WaitCursor;

                        pdu1 = new SmsSubmitPdu(SMS_Message, CELL_Number, "");

                        comm.SendMessage(pdu1);

                        Cursor.Current = Cursors.Default;

                        MessageBox.Show("M E S S A G E - S E N T", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }

                    catch (Exception E5)
                    {

                        MessageBox.Show("Error Sending SMS To Destination Address\r\n\n Connection Has Been Terminated !!!\r\n\n", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        comm.Close();
                        button9.Enabled = true;
                        button10.Enabled = false;
                        Single_SMS.Enabled = false;
                        dataGridView3.Rows.Clear();

                    }
                    
                }
                else
                {
                    MessageBox.Show("No GSM Phone / Modem Connected", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            catch (Exception E10)
            {
                MessageBox.Show("CONNECTION ERROR", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }           

        }

        private void Form1_Load(object sender, EventArgs e)
        {            
          
            button10.Enabled = false;
         
        }

        private void label30_DoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show(":-)\r\n\nLet The SMS Begin...!!!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        
        private void button9_Click(object sender, EventArgs e)
        {
            //Setting the column grid for fetched data

            dataGridView3.ColumnCount = 5;
            dataGridView3.Columns[0].Name = "COM Port";
            dataGridView3.Columns[1].Name = "Connected Device";
            dataGridView3.Columns[2].Name = "Max Baud Rate";
            dataGridView3.Columns[3].Name = "Time Out";
            dataGridView3.Columns[4].Name = "Status";
            dataGridView3.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            try
            {
                Cursor.Current = Cursors.WaitCursor;

                ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SerialPort");                                       

                foreach (ManagementObject queryObj in searcher.Get())
                {

                    if (queryObj != null)
                    {
                        object captionObj = queryObj["DESCRIPTION"];
                        object capdeviceid = queryObj["DEVICEID"];
                        object MaxBaudRate = queryObj["MAXBAUDRATE"];
                        object connstatus = queryObj["STATUS"];
                        string timeoutsec = "100";
                        dataGridView3.Rows.Add(capdeviceid, captionObj, MaxBaudRate, timeoutsec, connstatus);
                    }
                }

            }

            catch (Exception e15)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e15.Message);
            }

            Cursor.Current = Cursors.Default;
            button9.Enabled = false;
            button10.Enabled = true;
        }
        

        private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            DataGridViewCellStyle style = new DataGridViewCellStyle();

            style.Font = new Font(dataGridView3.Font, FontStyle.Bold);            
            style.BackColor = Color.Green;
            style.ForeColor = Color.White;
            

                int i = dataGridView3.CurrentRow.Index;
                

                try
                {
                    Comm_Port =     Convert.ToInt16(dataGridView3.Rows[i].Cells[0].Value.ToString().Substring(3));
                    Comm_BaudRate = Convert.ToInt32(dataGridView3.Rows[i].Cells[2].Value.ToString());
                    Comm_TimeOut =  Convert.ToInt32(dataGridView3.Rows[i].Cells[3].Value.ToString());
                }
                catch (Exception E1)
                {
                   // MessageBox.Show("Select a phone Model", "Check COM Port Values", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                comm = new GsmCommMain(Comm_Port, Comm_BaudRate, Comm_TimeOut);

                try
                {

                        comm.Open();
                        if (comm.IsConnected())
                        {

                          //  pictureBox3.Image = imageList1.Images[1];

                            MessageBox.Show("Connected Successfully To Mobile ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            dataGridView3.Rows[i].Cells[4].Value = "Connected";
                            dataGridView3.Rows[i].DefaultCellStyle = style;
                            dataGridView3.ClearSelection();
                            Single_SMS.Enabled = true;

                        }

                        try
                        {
                            //Phone_Name.Text = comm.IdentifyDevice().Manufacturer.ToUpper().ToString();
                            //Phone_Model.Text = comm.IdentifyDevice().Model.ToUpper().ToString();
                            //Revision_Num.Text = comm.IdentifyDevice().Revision.ToUpper().ToString();
                            //Serial_Num.Text = comm.IdentifyDevice().SerialNumber.ToUpper().ToString();
                        }
                        catch (Exception e50)
                        {
                            MessageBox.Show("Error Retriving COM Port Phone Information", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    

                }
                catch (Exception E2)
                {
                    //MessageBox.Show("Error While Connecting To GSM Phone / Modem", "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    dataGridView3.ClearSelection();

                }
        }


        private void button10_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (!comm.IsConnected())
                {
                    MessageBox.Show("No Phone Connected", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
                else
                {

                    comm.Close();
                
                    button9.Enabled = true;
                    button10.Enabled = false;
                    Single_SMS.Enabled = false;
                    dataGridView3.Rows.Clear();
                   
                   
                    MessageBox.Show("Disconnected Successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }

            catch (Exception No_Conn)
            {

                MessageBox.Show("No Phone Connected!!\r\n\n" + No_Conn, "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

            }
        }
        

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (comm.IsOpen() == true)
                {                    
                    comm.Close();
                }
            }
            catch (Exception E16)
            {               
                Application.Exit();
            }

            Application.Exit();
        }
        

        private void SMS_Text_TextChanged(object sender, EventArgs e)
        {
           // label4.Text = "Characters Left : "+(160 - SMS_Text.Text.Length).ToString();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void SMS_Text_Click(object sender, EventArgs e)
        {
            SMS_Text.Text = "";
        }

        private void comboBox1_Click(object sender, EventArgs e)
        {
            comboBox1.DroppedDown = true;
        }

        private void SMS_Text_Click_1(object sender, EventArgs e)
        {
            SMS_Text.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Single_SMS_Click(object sender, EventArgs e)
        {

            string CELL_Number, SMS_Message;

            SmsSubmitPdu pdu1;

            try
            {

                if (comm.IsConnected() == true)
                {
                    if (Cell_Num.Text.ToString() == "")
                    {
                        MessageBox.Show("Kindly Add Cell Number\r\n\nFor Example\n\n+9231234567", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }
                    else if (SMS_Text.Text.ToString() == "")
                    {
                        MessageBox.Show("Kindly Add Your Message Text", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        return;
                    }

                    CELL_Number = Cell_Num.Text.ToString();
                    SMS_Message = SMS_Text.Text.ToString();

                   // MessageBox.Show("Kindly Re-Check Your SMS & CELL NUMBER \r\n\nCELL # : " + CELL_Number + "\r\n\nMESSAGE TEXT : " + SMS_Message, "SMS INFO", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                    try
                    {
                        Cursor.Current = Cursors.WaitCursor;

                        pdu1 = new SmsSubmitPdu(SMS_Message, CELL_Number, "");

                        comm.SendMessage(pdu1);

                        Cursor.Current = Cursors.Default;

                        MessageBox.Show("Message Sent Successfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }

                    catch (Exception E5)
                    {

                        MessageBox.Show("Error Sending SMS To Destination Address\r\n\n Connection Has Been Terminated !!!\r\n\n", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        comm.Close();
                    
                        button9.Enabled = true;
                        button10.Enabled = false;
                        Single_SMS.Enabled = false;
                      

                    }


                }
                else
                {
                    MessageBox.Show("No GSM Phone / Modem Connected", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            catch (Exception E10)
            {
                MessageBox.Show("CONNECTION ERROR", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }           

        }

        private void Single_SMS_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                Single_SMS.PerformClick();
              
            }
        }
        }
    }

No comments:

Post a Comment

Sending SMS Via Windows Form Application Using any Android Smart Phone  Source Code  using System ; using System . C...