C#中的批量SMS发件人

主启动屏幕

批量短信发送器主屏幕

介绍

使用免费的GSM通信库GSMComm的简单SMS应用程序,用于使用我们所有消息和收件人号码的Excel文件将批量SMS发送到多个收件人

背景

对于学习非常有帮助...如何以最少的努力使用免费的GSM通信库通过C#发送SMS :-)

使用代码

现在,让我们继续实际的代码,第一步,需要将GSM Phone / GSM调制解调器连接到PC,假设您已将设备连接到PC,并且所有必要的驱动程序都已加载并且已成功连接。

要使用GSM通信库连接设备,您必须从http://www.scampers.org下载GSM COM LIb并在您的项目中引用它,然后在您的项目中使用它


using GsmComm.GsmCommunication;
using GsmComm.Interfaces;
using GsmComm.PduConverter;
using GsmComm.Server; 
并构造用于连接和GSMComm类的初始必要信息。

        public static Int16 Comm_Port = 0;
        public static Int32 Comm_BaudRate = 0;
        public static Int32 Comm_TimeOut = 0;
        public static GsmCommMain comm; 
将设备连接到PC后运行Bingo,然后运行该项目,并出现上面显示的主屏幕,现在按主屏幕上的Get COM Port List按钮将所有端口信息输入到DataGrid中

系统中的所有COM端口列表将显示在网格框中,如上所示

为了获取COM端口列表和COM端口的其他数据,Mircosoft向我们提供了一个名为“ WMI CODE CREATOR”的实用程序,可以轻松地为此目的创建C#代码Google It并将其下载到您的PC,它将为您提供成千上万的类/方法/通过最少的系统编程工作即可获取系统信息的属性。

在这里,我如何获取所有COM端口信息以及所有其他设备信息

//

//在这里,我们从Win32_SerialPort获取所有可用信息,并将其设置为DataGrid


        try
            {
                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);
                    }}}  

如果您没有从系统中获取所有COM端口信息,则第二步是连接到设备,单击所需设备的数据网格单元以进行连接,如下图所示。将出现确认消息并已连接设备信息也将显示,DataGrid单元将突出显示为绿色。

如何连接到GSM设备/电话,单击“单元格”时尝试从数据网格获取值的GSMComm连接

 
//FOR GsmCommMain we give three arguments (PORT , BAUD RATE , TIMEOUT SEC) 
                    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()); 
                    comm = new GsmCommMain(Comm_Port, Comm_BaudRate, Comm_TimeOut); 
                try
                {
                        comm.Open();
                        if (comm.IsConnected())
                        {
                //Something to do when device connected
                        } 
Getting Phone Information after successfull connection 
//Getting phone information through IdentificationInfo Structures of GSMComm ... 
                        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();
                        } 
与设备成功连接后...现在该发送一个单词了:-)

让我们签出单个短信发送标签


//For sending single SMS with minimal code  
//For SmsSubmitPdu we give three arguments (SMS TEXT , RECIPIENTS NUMBER , ENCODING ) 
                string CELL_Number, SMS_Message; 
                SmsSubmitPdu pdu1; 
                    CELL_Number = Cell_Num.Text.ToString();
                    SMS_Message = SMS_Text.Text.ToString(); 
                    try
                    {
                        pdu1 = new SmsSubmitPdu(SMS_Message, CELL_Number, "");
                        comm.SendMessage(pdu1);
                        MessageBox.Show("M E S S A G E - S E N T", "Information", MessageBoxButtons.OK, 
                MessageBoxIcon.Information); 
                    } 
要从Excel文件发送多个SMS,请按以下步骤操作。

//单击按钮下面的代码将打开Excel文件的“文件选择对话框”,其中仅包含工作表名称SMS

两列名为CELL NUMBER和MESSAGES的列将被加载到DataGridView中,

另外,您还需要将Excel工作表重命名为SMS,因为它还会检查工作表名称


        private void button3_Click_1(object sender, EventArgs e)
        {
            int rows_counting, column_counting1 = 0;
            OpenFileDialog dialog = new OpenFileDialog { };
            dialog.Filter = "SMS Sending File(*.xlsx;*.xls)|*.xlsx;*.xls";
            dialog.Title = "Select Excel File For SMS";
            DialogResult dlgresult = dialog.ShowDialog();
            if (dlgresult == DialogResult.Cancel)
            {
                MessageBox.Show("You Cancelled !!!");
            }
            else
            {
                string sms_filename = dialog.FileName; 
                if (System.IO.File.Exists(sms_filename))
                {
                    try
                    {
                        Cursor.Current = Cursors.WaitCursor; 
                        string connectionString = String.Format(
                @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};
                Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", 
                sms_filename); 
                        string query = String.Format("select * from [{0}$]", "SMS");
                        OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
                        dataSet = new DataSet();
                        dataAdapter.Fill(dataSet);
                        dataGridView1.DataSource = dataSet.Tables[0];
                        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
                        rows_counting = dataGridView1.RowCount - 1;                        
                        column_counting1 = dataGridView1.ColumnCount; 
                        if (column_counting1 < 2 || column_counting1 > 2)
                        {
                            MessageBox.Show("Kindly Check Column Count in Excel Sheet !!!\r\n\n
                               There Should Be Only Two Columns in Sheet Like Below\r\n\n
                               CELL NUMBER | MESSAGE", "Error", MessageBoxButtons.OK, 
                               MessageBoxIcon.Error);                            
                            return;
                        } 
                        if (    dataGridView1.Columns[0].Name.ToString().ToUpper() == "CELL NUMBER" &&
                                dataGridView1.Columns[1].Name.ToString().ToUpper() == "MESSAGES") 
                        {
                            label25.Text = "Total SMS In Excel File " + rows_counting;
                            MessageBox.Show("Data Imported Successfully...!!!\r\n\n
                               Check Imported Values & SEND SMS.....!!!!", 
                              "Information", MessageBoxButtons.OK, 
                               MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("Column Names Are Not In Specified Format !!!", 
                              "Error", MessageBoxButtons.OK, 
                               MessageBoxIcon.Error);
                            return;
                        }                        
                    }
                    catch (Exception E6)
                    {
                        MessageBox.Show("Error Loading Excel FIle\r\n\nKindly Check Worksheet Name", 
                            "Error", MessageBoxButtons.OK, 
                             MessageBoxIcon.Error);
                        return;
                    }
            }}} 
将Excel文件加载到DataGrid后,我们将仅使用For Loop函数来获取CELL NUMBER和MESSAGE一对一并使用非常简单的代码发送,就像发送单个SMS消息一样

        string MSMS_Number, MMessage;
            int i;
            SmsSubmitPdu pdu3;
            try
            {
                if (comm.IsConnected()==true)
                    {
                        try
                        {
                            for (i = 0; i < dataGridView1.RowCount - 1; i++)
                            { 
                                MSMS_Number = dataGridView1.Rows[i].Cells[0].Value.ToString();
                                MMessage = dataGridView1.Rows[i].Cells[1].Value.ToString();
                                pdu3 = new SmsSubmitPdu(MMessage, MSMS_Number, "");
                                comm.SendMessage(pdu3);
                                //Sleeps system for 1000ms for refreshing GSM Modem / Phone
                                System.Threading.Thread.Sleep(1000);
                            } 
                        Cursor.Current = Cursors.Default; 
                        MessageBox.Show("T O T A L - M E S S A G E - S E N T = " + i,
                            "Information", MessageBoxButtons.OK, 
                             MessageBoxIcon.Information);
                        } 
简单不是....如果不给我发个问题,我很乐意为您提供帮助:-)

对于额外的香料,我还添加了简单的检查,当单击“检查值”按钮时,将Excel文件加载到DataGridView中时,它将检查NULL值和消息文本是否超过160个字符,如果发现错误,则将单元格突出显示为红色,否则,如果为绿色,则单元格为绿色。没有发现错误。

为了完成单元格检查,再次进行一次简单的FOR LOOP将完成Magic ;-)

//在代码下方的“检查值”按钮上,将一一检查单元格值并设置红色或绿色

如果IF语句通过


       private void button7_Click(object sender, EventArgs e)
        { 
            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                for (int j = 0; j < dataGridView1.ColumnCount ; j++)
                {
                    if (    dataGridView1.Rows[i].Cells[j].Value.ToString() == "" ||                        
                            dataGridView1.Rows[i].Cells[j].Value.ToString().ToUpper() == "-" ||
                            dataGridView1.Rows[i].Cells[j].Value.ToString().Length > 160 ) 
                        //Setting Cellls Background color to RED if above Error Found in Any of the cells 
                        dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Red; 
                        //Setting Cells Background color to GREEN which passes above validations 
                    else 
                        dataGridView1.Rows[i].Cells[j].Style.BackColor = Color.Green;
                        button4.Enabled = true;
                }}} 
多数民众赞成......幸福的编码…………让短信开始!!! :-) 兴趣点

您应该尝试WMI CODE CREATOR,它将为探索系统信息提供很多帮助

历史

实际上,这是为大型项目而编写的较小版本的代码,用于某些特定目的,以便为团队成员发送批量SMS :-)

如果您想获得任何帮助,我们将非常高兴。 批量短信发送器Ver 1.0
附加的文件
文件类型:zip SMS Sender Program.zip (313.7 KB,2489视图)
文件类型:xlsx SMS.xlsx (8.4 KB,1310视图)
文件类型:zip SMS_SENDER_SOURCE_CODE.zip (659.1 KB,3359视图)

From: https://bytes.com/topic/c-sharp/insights/931771-bulk-sms-sender-c

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值