Ahmet Taha

Ahmet Taha

  • 1.7k
  • 363
  • 7.2k

sending sms to many numbers using gsm modem in c#

Jun 5 2021 1:13 AM
Hello Guyz, i would like to ask how can i send sms to all number attached here is my source code i can only sent a sms to one phone number but i want to send it to many phone number. thanks  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.Data.SqlClient;  
  10.   
  11. //============  
  12. using System.IO.Ports;  
  13. using System.Threading;  
  14.   
  15. namespace StudentInformationSystem  
  16. {  
  17.     public partial class frmsendingsms : Form  
  18.     {  
  19.         SqlCommand cmd;  
  20.         SqlConnection cn;  
  21.         SqlDataAdapter da;  
  22.         SqlDataReader dr;  
  23.         string _title = "STUDENT INFORMATION SYSTEM";  
  24.         public frmsendingsms()  
  25.         {  
  26.             InitializeComponent();  
  27.             academicyear();  
  28.         }  
  29.         public void academicyear()  
  30.         {  
  31.   
  32.             cn = ClassDB.ConnectDB();  
  33.             //cn.Open();  
  34.             cmd = new SqlCommand("select academicyear from tblAY where status like 'OPEN'", cn);  
  35.             cmd.Parameters.AddWithValue("@academicyear", txtacademicyear.Text);  
  36.             dr = cmd.ExecuteReader();  
  37.             dr.Read();  
  38.             if (dr.HasRows)  
  39.             {  
  40.                 txtacademicyear.Text = dr["academicyear"].ToString();  
  41.                 // txtacademicterm.Text = dr["term"].ToString();  
  42.                 //cbacademicterm.Text = dr["term"].ToString();  
  43.             }  
  44.             else  
  45.             {  
  46.   
  47.             }  
  48.   
  49.             dr.Close();  
  50.             cn.Close();  
  51.   
  52.         }  
  53.   
  54.         private void btnSend_Click(object sender, EventArgs e)  
  55.         {  
  56.             try  
  57.             {  
  58.                 SerialPort sp = new SerialPort();  
  59.                 sp.PortName = cbportnumber.Text;  
  60.                 sp.Open();  
  61.                 sp.WriteLine("AT" + Environment.NewLine);  
  62.                 Thread.Sleep(100);  
  63.   
  64.                 sp.WriteLine("AT+CMGF=1" + Environment.NewLine);  
  65.                 Thread.Sleep(100);  
  66.   
  67.                 sp.WriteLine("AT+CMGS=\"" + txtPhoneNumber.Text + "\"" + Environment.NewLine);  
  68.                 Thread.Sleep(100);  
  69.   
  70.                 sp.WriteLine(txtMessage.Text + Environment.NewLine);  
  71.                 Thread.Sleep(100);  
  72.   
  73.                 sp.Write(new byte[] { 26 }, 0, 1);  
  74.                 Thread.Sleep(100);  
  75.   
  76.                 var response = sp.ReadExisting();  
  77.                 if (response.Contains("ERROR"))  
  78.                     MessageBox.Show("Send failed!""Message", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  79.                 else  
  80.                     MessageBox.Show("Message has been sent""Message", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  81.                 sp.Close();  
  82.   
  83.             }  
  84.             catch (Exception ex)  
  85.             {  
  86.                 MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  87.             }  
  88.         }  
  89.   
  90.         private void txtyear1_TextChanged(object sender, EventArgs e)  
  91.         {  
  92.             try  
  93.             {  
  94.                 txtyear2.Text = (long.Parse(txtyear1.Text) + 1).ToString();  
  95.             }  
  96.             catch  
  97.             {  
  98.                 txtyear2.Clear();  
  99.             }  
  100.             txtacademicyear.Text = txtyear1.Text + "-" + txtyear2.Text;  
  101.         }  
  102.   
  103.         private void txtsearch_KeyUp(object sender, KeyEventArgs e)  
  104.         {  
  105.             cn = ClassDB.ConnectDB();  
  106.             listBox1.Visible = true;  
  107.             listBox1.Items.Clear();  
  108.             SqlCommand cmd = cn.CreateCommand();  
  109.             cmd.CommandType = CommandType.Text;  
  110.             cmd = new SqlCommand("Select* from tblstudentregistration where lrn like'" + txtsearch.Text + "%' or fullname Like '" + txtsearch.Text + "%'", cn);  
  111.             cmd.ExecuteNonQuery();  
  112.             DataTable dt = new DataTable();  
  113.             SqlDataAdapter da = new SqlDataAdapter(cmd);  
  114.             da.Fill(dt);  
  115.             foreach (DataRow dr in dt.Rows)  
  116.             {  
  117.                 listBox1.Items.Clear();  
  118.                 listBox1.Items.Add(dr["lrn"]).ToString();  
  119.                 //  listBox1.Items.Clear();  
  120.                 listBox1.Items.Add(dr["fullname"]).ToString();  
  121.             }  
  122.         }  
  123.   
  124.         private void txtsearch_KeyDown(object sender, KeyEventArgs e)  
  125.         {  
  126.             try  
  127.             {  
  128.                 cn = ClassDB.ConnectDB();  
  129.                 if (e.KeyCode == Keys.Down)  
  130.                 {  
  131.                     listBox1.Focus();  
  132.                     listBox1.SelectedIndex = 0;  
  133.                 }  
  134.             }  
  135.             catch (Exception ex)  
  136.             {  
  137.                 MessageBox.Show(ex.ToString());  
  138.             }  
  139.         }  
  140.   
  141.         private void listBox1_KeyDown(object sender, KeyEventArgs e)  
  142.         {  
  143.             cn = ClassDB.ConnectDB();  
  144.             if (e.KeyCode == Keys.Down)  
  145.             {  
  146.                 this.listBox1.SelectedIndex = this.listBox1.SelectedIndex;  
  147.             }  
  148.             if (e.KeyCode == Keys.Up)  
  149.             {  
  150.                 this.listBox1.SelectedIndex = this.listBox1.SelectedIndex;  
  151.             }  
  152.             if (e.KeyCode == Keys.Enter)  
  153.             {  
  154.                 //search();  
  155.                 txtsearch.Text = listBox1.SelectedItem.ToString();  
  156.                 displayresulttextboxs();  
  157.                 // displayresultontxtboxes();  
  158.                 listBox1.Visible = false;  
  159.   
  160.                  
  161.   
  162.             }  
  163.         }  
  164.         private void displayresulttextboxs()  
  165.         {  
  166.             cn = ClassDB.ConnectDB();  
  167.             // cmd = new SqlCommand("Select * from tblstudentregistration where lrn='" + txtsearchstudentlrn0rFullname.Text + "' and academicyear='" + txtacademicyear.Text + "' and academicterm='" + txtacademicterm.Text + "'", cn);  
  168.             cmd = new SqlCommand("Select * from tblstudentregistration where lrn='" + txtsearch.Text + "' and academicyear='"+txtacademicyear.Text+"' and academicterm ='"+cbacademicterm.Text+"'", cn);  
  169.             dr = cmd.ExecuteReader();  
  170.             if (dr.Read())  
  171.             {  
  172.                 txtsearch.Text = dr[1].ToString();  
  173.                txtPhoneNumber.Text= dr[9].ToString();  
  174.                  
  175.             }  
  176.   
  177.             else  
  178.             {  
  179.                 cn = ClassDB.ConnectDB();  
  180.                 // cmd = new SqlCommand("Select * from tblstudentregistration where fullname='" + txtsearchstudentlrn0rFullname.Text + "' and academicyear='" + txtacademicyear.Text + "' and academicterm='" + txtacademicterm.Text + "'", cn);  
  181.                 cmd = new SqlCommand("Select * from tblstudentregistration where fullname='" + txtsearch.Text + "' and academicyear='" + txtacademicyear.Text + "' and academicterm ='" + cbacademicterm.Text + "'", cn);  
  182.                 dr = cmd.ExecuteReader();  
  183.                 if (dr.Read())  
  184.                 {  
  185.                     txtsearch.Text = dr[1].ToString();  
  186.                     txtPhoneNumber.Text = dr[9].ToString();  
  187.                  
  188.   
  189.                 }  
  190.                 else  
  191.                 {  
  192.                     MessageBox.Show("There's no Record has been found", _title, MessageBoxButtons.OK, MessageBoxIcon.Stop);  
  193.                 }  
  194.             }  
  195.         }  
  196.   
  197.         private void btnsearch_Click(object sender, EventArgs e)  
  198.         {  
  199.   
  200.         }  
  201.     }  
  202. }  
 

Answers (2)