首页 > 开发 > 综合 > 正文

再送一份,C#发送短信源码!

2024-07-21 02:27:32
字体:
来源:转载
供稿:网友
今年三月份,用vb,c#各写一份用手机+电脑+数据线发送短信的程式!感觉还行。 以下的功能单一,只能单发,或群发。只是初始的版本。朋友们可以看看其中的 一些技巧,如怎么将进值转换,中文转换,字符移位处理。用mscomm怎么发送和接收 数据,怎么延时等! using system; using system.drawing; using system.collections; using system.componentmodel; using system.windows.forms; using system.data; using system.threading; namespace sendsms {    ///    /// form1 的摘要说明。    ///    public class frmmain : system.windows.forms.form    {       #region 控件的初始化=====================================================       private system.windows.forms.groupbox groupbox1;       private system.windows.forms.combobox cbobox2;       private system.windows.forms.combobox cbobox1;       private system.windows.forms.button btncnn;       private system.windows.forms.label label2;       private system.windows.forms.label lbl;       private system.windows.forms.label lbls;       private system.windows.forms.button btnclose;       private system.windows.forms.label lblcnninfo;       private axmscommlib.axmscomm mscomm;       private system.windows.forms.groupbox groupbox2;       private system.windows.forms.label lblcenter;       private system.windows.forms.textbox txtcenter;       private system.windows.forms.label lblvirtualdate;       private system.windows.forms.label lblmobile;       private system.windows.forms.textbox txtmobile;       private system.windows.forms.label label1;       private system.windows.forms.statusbar statusbar1;       private system.windows.forms.statusbarpanel statusbarpanel1;       private system.windows.forms.statusbarpanel statusbarpanel2;       private system.windows.forms.statusbarpanel statusbarpanel3;       private system.windows.forms.button btnsendsms;       private system.windows.forms.textbox txtsms;       private system.windows.forms.combobox cbobox3;       private system.windows.forms.label lblsms;       private system.windows.forms.textbox txtinput;       ///       /// 必需的设计器变量。       ///       private system.componentmodel.container components = null;       public frmmain()       {          //          // windows 窗体设计器支持所必需的          //          initializecomponent();          //          // tod 在 initializecomponent 调用后添加任何构造函数代码          //       }       ///       /// 清理所有正在使用的资源。       ///       protected override void dispose( bool disposing )       {          if( disposing )          {             if (components != null)             {                components.dispose();             }          }          base.dispose( disposing );       }       #region windows form designer generated code       ///       /// 设计器支持所需的方法 - 不要使用代码编辑器修改       /// 此方法的内容。       ///       private void initializecomponent()       {          system.resources.resourcemanager resources = new system.resources.resourcemanager(typeof(frmmain));          this.groupbox1 = new system.windows.forms.groupbox();          this.mscomm = new axmscommlib.axmscomm();          this.lblcnninfo = new system.windows.forms.label();          this.btnclose = new system.windows.forms.button();          this.cbobox2 = new system.windows.forms.combobox();          this.cbobox1 = new system.windows.forms.combobox();          this.btncnn = new system.windows.forms.button();          this.label2 = new system.windows.forms.label();          this.lbl = new system.windows.forms.label();          this.lbls = new system.windows.forms.label();          this.groupbox2 = new system.windows.forms.groupbox();          this.txtinput = new system.windows.forms.textbox();          this.lblsms = new system.windows.forms.label();          this.cbobox3 = new system.windows.forms.combobox();          this.btnsendsms = new system.windows.forms.button();          this.txtsms = new system.windows.forms.textbox();          this.label1 = new system.windows.forms.label();          this.txtmobile = new system.windows.forms.textbox();          this.lblmobile = new system.windows.forms.label();          this.lblvirtualdate = new system.windows.forms.label();          this.txtcenter = new system.windows.forms.textbox();          this.lblcenter = new system.windows.forms.label();          this.statusbar1 = new system.windows.forms.statusbar();          this.statusbarpanel1 = new system.windows.forms.statusbarpanel();          this.statusbarpanel2 = new system.windows.forms.statusbarpanel();          this.statusbarpanel3 = new system.windows.forms.statusbarpanel();          this.groupbox1.suspendlayout();          ((system.componentmodel.isupportinitialize)(this.mscomm)).begininit();          this.groupbox2.suspendlayout();          ((system.componentmodel.isupportinitialize)(this.statusbarpanel1)).begininit();          ((system.componentmodel.isupportinitialize)(this.statusbarpanel2)).begininit();          ((system.componentmodel.isupportinitialize)(this.statusbarpanel3)).begininit();          this.suspendlayout();          //          // groupbox1          //          this.groupbox1.controls.add(this.mscomm);          this.groupbox1.controls.add(this.lblcnninfo);          this.groupbox1.controls.add(this.btnclose);          this.groupbox1.controls.add(this.cbobox2);          this.groupbox1.controls.add(this.cbobox1);          this.groupbox1.controls.add(this.btncnn);          this.groupbox1.controls.add(this.label2);          this.groupbox1.controls.add(this.lbl);          this.groupbox1.controls.add(this.lbls);          this.groupbox1.location = new system.drawing.point(8, 8);          this.groupbox1.name = "groupbox1";          this.groupbox1.size = new system.drawing.size(192, 328);          this.groupbox1.tabindex = 1;          this.groupbox1.tabstop = false;          this.groupbox1.text = "通讯设置";          //          // mscomm          //          this.mscomm.containingcontrol = this;          this.mscomm.enabled = true;          this.mscomm.location = new system.drawing.point(120, 264);          this.mscomm.name = "mscomm";          this.mscomm.ocxstate = ((system.windows.forms.axhost.state)(resources.getobject("mscomm.ocxstate")));          this.mscomm.size = new system.drawing.size(38, 38);          this.mscomm.tabindex = 9;          this.mscomm.oncomm += new system.eventhandler(this.mscomm_oncomm);          //          // lblcnninfo          //          this.lblcnninfo.autosize = true;          this.lblcnninfo.location = new system.drawing.point(8, 208);          this.lblcnninfo.name = "lblcnninfo";          this.lblcnninfo.size = new system.drawing.size(0, 17);          this.lblcnninfo.tabindex = 8;          //          // btnclose          //          this.btnclose.enabled = false;          this.btnclose.flatstyle = system.windows.forms.flatstyle.popup;          this.btnclose.forecolor = system.drawing.systemcolors.controltext;          this.btnclose.location = new system.drawing.point(96, 152);          this.btnclose.name = "btnclose";          this.btnclose.tabindex = 6;          this.btnclose.text = "断开(&d)";          this.btnclose.click += new system.eventhandler(this.btnclose_click);          //          // cbobox2          //          this.cbobox2.backcolor = system.drawing.color.fromargb(((system.byte)(224)), ((system.byte)(224)), ((system.byte)(224)));          this.cbobox2.cursor = system.windows.forms.cursors.pansouth;          this.cbobox2.dropdownstyle = system.windows.forms.comboboxstyle.dropdownlist;          this.cbobox2.items.addrange(new object[] {                                            "2400",                                            "4800",                                            "9600",                                            "19200",                                            "38400",                                            "57600",                                            "115200"});          this.cbobox2.location = new system.drawing.point(78, 96);          this.cbobox2.name = "cbobox2";          this.cbobox2.size = new system.drawing.size(90, 20);          this.cbobox2.tabindex = 5;          //          // cbobox1          //          this.cbobox1.backcolor = system.drawing.color.fromargb(((system.byte)(224)), ((system.byte)(224)), ((system.byte)(224)));          this.cbobox1.cursor = system.windows.forms.cursors.pansouth;          this.cbobox1.dropdownstyle = system.windows.forms.comboboxstyle.dropdownlist;          this.cbobox1.items.addrange(new object[] {                                            "com1",                                            "com2",                                            "com3",                                            "com4",                                            "com5",                                            "com6"});          this.cbobox1.location = new system.drawing.point(78, 64);          this.cbobox1.name = "cbobox1";          this.cbobox1.size = new system.drawing.size(90, 20);          this.cbobox1.tabindex = 4;          //          // btncnn          //          this.btncnn.flatstyle = system.windows.forms.flatstyle.popup;          this.btncnn.location = new system.drawing.point(8, 152);          this.btncnn.name = "btncnn";          this.btncnn.tabindex = 3;          this.btncnn.text = "连接(&c)";          this.btncnn.click += new system.eventhandler(this.btncnn_click);          //          // label2          //          this.label2.autosize = true;          this.label2.location = new system.drawing.point(14, 96);          this.label2.name = "label2";          this.label2.size = new system.drawing.size(66, 17);          this.label2.tabindex = 2;          this.label2.text = "通讯速率:";          //          // lbl          //          this.lbl.autosize = true;          this.lbl.location = new system.drawing.point(14, 72);          this.lbl.name = "lbl";          this.lbl.size = new system.drawing.size(66, 17);          this.lbl.tabindex = 1;          this.lbl.text = "通讯端口:";          //          // lbls          //          this.lbls.forecolor = system.drawing.color.red;          this.lbls.location = new system.drawing.point(14, 23);          this.lbls.name = "lbls";          this.lbls.size = new system.drawing.size(144, 32);          this.lbls.tabindex = 0;          this.lbls.text = "请选择正确的通讯端口,通讯速率一般为19200。";          //          // groupbox2          //          this.groupbox2.controls.add(this.txtinput);          this.groupbox2.controls.add(this.lblsms);          this.groupbox2.controls.add(this.cbobox3);          this.groupbox2.controls.add(this.btnsendsms);          this.groupbox2.controls.add(this.txtsms);          this.groupbox2.controls.add(this.label1);          this.groupbox2.controls.add(this.txtmobile);          this.groupbox2.controls.add(this.lblmobile);          this.groupbox2.controls.add(this.lblvirtualdate);          this.groupbox2.controls.add(this.txtcenter);          this.groupbox2.controls.add(this.lblcenter);          this.groupbox2.location = new system.drawing.point(200, 8);          this.groupbox2.name = "groupbox2";          this.groupbox2.size = new system.drawing.size(312, 328);          this.groupbox2.tabindex = 2;          this.groupbox2.tabstop = false;          this.groupbox2.text = "短信";          //          // txtinput          //          this.txtinput.backcolor = system.drawing.color.fromargb(((system.byte)(224)), ((system.byte)(224)), ((system.byte)(224)));          this.txtinput.enabled = false;          this.txtinput.location = new system.drawing.point(200, 40);          this.txtinput.multiline = true;          this.txtinput.name = "txtinput";          this.txtinput.size = new system.drawing.size(96, 120);          this.txtinput.tabindex = 11;          this.txtinput.text = "";          //          // lblsms          //          this.lblsms.autosize = true;          this.lblsms.forecolor = system.drawing.color.red;          this.lblsms.location = new system.drawing.point(16, 304);          this.lblsms.name = "lblsms";          this.lblsms.size = new system.drawing.size(0, 17);          this.lblsms.tabindex = 10;          //          // cbobox3          //          this.cbobox3.backcolor = system.drawing.color.fromargb(((system.byte)(224)), ((system.byte)(224)), ((system.byte)(224)));          this.cbobox3.cursor = system.windows.forms.cursors.pansouth;          this.cbobox3.dropdownstyle = system.windows.forms.comboboxstyle.dropdownlist;          this.cbobox3.items.addrange(new object[] {                                            "00 - 没有 ",                                            "47 - 6小时",                                            "90 - 12小时",                                            "a7 - 1天",                                            "a8 - 2天",                                            "a9 - 3天",                                            "aa - 4天",                                            "ab - 5天",                                            "ac - 6天",                                            "ad - 7天"});          this.cbobox3.location = new system.drawing.point(16, 88);          this.cbobox3.name = "cbobox3";          this.cbobox3.size = new system.drawing.size(176, 20);          this.cbobox3.tabindex = 9;          //          // btnsendsms          //          this.btnsendsms.enabled = false;          this.btnsendsms.flatstyle = system.windows.forms.flatstyle.popup;          this.btnsendsms.forecolor = system.drawing.systemcolors.controltext;          this.btnsendsms.location = new system.drawing.point(184, 296);          this.btnsendsms.name = "btnsendsms";          this.btnsendsms.tabindex = 8;          this.btnsendsms.text = "发送(&s)";          this.btnsendsms.click += new system.eventhandler(this.btnsendsms_click);          //          // txtsms          //          this.txtsms.backcolor = system.drawing.color.fromargb(((system.byte)(224)), ((system.byte)(224)), ((system.byte)(224)));          this.txtsms.location = new system.drawing.point(16, 184);          this.txtsms.multiline = true;          this.txtsms.name = "txtsms";          this.txtsms.size = new system.drawing.size(288, 104);          this.txtsms.tabindex = 7;          this.txtsms.text = "";          //          // label1          //          this.label1.autosize = true;          this.label1.location = new system.drawing.point(16, 168);          this.label1.name = "label1";          this.label1.size = new system.drawing.size(208, 17);          this.label1.tabindex = 6;          this.label1.text = "短信内容:(内容不能超过104个字符)";          //          // txtmobile          //          this.txtmobile.backcolor = system.drawing.color.fromargb(((system.byte)(224)), ((system.byte)(224)), ((system.byte)(224)));          this.txtmobile.location = new system.drawing.point(16, 136);          this.txtmobile.maxlength = 11;          this.txtmobile.name = "txtmobile";          this.txtmobile.size = new system.drawing.size(176, 21);          this.txtmobile.tabindex = 5;          this.txtmobile.text = "";          this.txtmobile.keypress += new system.windows.forms.keypresseventhandler(this.txtmobile_keypress);          //          // lblmobile          //          this.lblmobile.autosize = true;          this.lblmobile.location = new system.drawing.point(16, 120);          this.lblmobile.name = "lblmobile";          this.lblmobile.size = new system.drawing.size(178, 17);          this.lblmobile.tabindex = 4;          this.lblmobile.text = "发送手机号:(例:13121011900)";          //          // lblvirtualdate          //          this.lblvirtualdate.autosize = true;          this.lblvirtualdate.location = new system.drawing.point(16, 72);          this.lblvirtualdate.name = "lblvirtualdate";          this.lblvirtualdate.size = new system.drawing.size(79, 17);          this.lblvirtualdate.tabindex = 2;          this.lblvirtualdate.text = "短信有效期:";          //          // txtcenter          //          this.txtcenter.backcolor = system.drawing.color.fromargb(((system.byte)(224)), ((system.byte)(224)), ((system.byte)(224)));          this.txtcenter.forecolor = system.drawing.systemcolors.controltext;          this.txtcenter.location = new system.drawing.point(16, 40);          this.txtcenter.maxlength = 14;          this.txtcenter.name = "txtcenter";          this.txtcenter.size = new system.drawing.size(176, 21);          this.txtcenter.tabindex = 1;          this.txtcenter.text = "+8613010112500";          this.txtcenter.keypress += new system.windows.forms.keypresseventhandler(this.txtcenter_keypress);          //          // lblcenter          //          this.lblcenter.autosize = true;          this.lblcenter.location = new system.drawing.point(16, 24);          this.lblcenter.name = "lblcenter";          this.lblcenter.size = new system.drawing.size(208, 17);          this.lblcenter.tabindex = 0;          this.lblcenter.text = "短信中心号码:(例:+8613010112500)";          //          // statusbar1          //          this.statusbar1.cursor = system.windows.forms.cursors.hand;          this.statusbar1.location = new system.drawing.point(0, 339);          this.statusbar1.name = "statusbar1";          this.statusbar1.panels.addrange(new system.windows.forms.statusbarpanel[] {                                                                    this.statusbarpanel1,                                                                    this.statusbarpanel2,                                                                    this.statusbarpanel3});          this.statusbar1.showpanels = true;          this.statusbar1.size = new system.drawing.size(518, 24);          this.statusbar1.tabindex = 3;          //          // statusbarpanel1          //          this.statusbarpanel1.autosize = system.windows.forms.statusbarpanelautosize.contents;          this.statusbarpanel1.icon = ((system.drawing.icon)(resources.getobject("statusbarpanel1.icon")));          this.statusbarpanel1.width = 31;          //          // statusbarpanel2          //          this.statusbarpanel2.autosize = system.windows.forms.statusbarpanelautosize.contents;          this.statusbarpanel2.width = 10;          //          // statusbarpanel3          //          this.statusbarpanel3.width = 300;          //          // frmmain          //          this.autoscalebasesize = new system.drawing.size(6, 14);          this.clientsize = new system.drawing.size(518, 363);          this.controls.add(this.statusbar1);          this.controls.add(this.groupbox2);          this.controls.add(this.groupbox1);          this.formborderstyle = system.windows.forms.formborderstyle.fixed3d;          this.maximizebox = false;          this.name = "frmmain";          this.startposition = system.windows.forms.formstartposition.centerscreen;          this.text = "短信群发系统..";          this.load += new system.eventhandler(this.frmmain_load);          this.groupbox1.resumelayout(false);          ((system.componentmodel.isupportinitialize)(this.mscomm)).endinit();          this.groupbox2.resumelayout(false);          ((system.componentmodel.isupportinitialize)(this.statusbarpanel1)).endinit();          ((system.componentmodel.isupportinitialize)(this.statusbarpanel2)).endinit();          ((system.componentmodel.isupportinitialize)(this.statusbarpanel3)).endinit();          this.resumelayout(false);       }       #endregion              ///       /// 应用程序的主入口点。       ///       [stathread]       static void main()       {          application.run(new frmmain());       }       private void frmmain_load(object sender, system.eventargs e)       {          this.cbobox1.text="com1";          this.cbobox2.text="19200";          this.cbobox3.text="a7 - 1天";          statusbar();       }       #endregion       #region 手机与电脑连接和断开事件=========================================       private void btncnn_click(object sender, system.eventargs e)       {                    lblcnninfo.text="";          btnclose.enabled =false;                 if (mscomm.portopen)          {             messagebox.show("端口已打开!","提示信息");          }          else          {                mscomm.portopen =true;             lblcnninfo.text="com1已打开!";          }                    mscomm.inbuffercount = 0;          string strtemp="at/r";          int t = environment.tickcount;          int l;          string buffer = "";          mscomm.output = strtemp;                                 do             {             system.windows.forms.application.doevents();             console.writeline(environment.tickcount - t );             lblcnninfo.text = "com1 已打开," +"/r"+ "正在连接手机 ...";             buffer += mscomm.input;             l=buffer.indexof("ok/r/n");          } while(l == -1 && environment.tickcount - t <=2500);                    if (l!=-1)          {             btncnn.enabled=false;             btnclose.enabled =true;             btnsendsms.enabled =true;             lblcnninfo.text = "com1 已打开," + (char)13 + "连接手机成功!";             }          else          {             btncnn.enabled =true;             btnclose.enabled =false;             mscomm.portopen =false;             btnsendsms.enabled =false;             lblcnninfo.text = "com1 已打开," + (char)13 + "连接手机失败!";                          }       }       private void btnclose_click(object sender, system.eventargs e)       {          if (mscomm.portopen)          {             mscomm.portopen =false;          }          btncnn.enabled =true;          btnclose.enabled =false;          btnsendsms.enabled =false;          lblcnninfo.text = "com1关闭, 已断开手机的连接!";       } #endregion          #region 发送短信=========================================================       private void btnsendsms_click(object sender, system.eventargs e)       {          lblsms.text="";          if(this.txtcenter.text.trim()=="")          {                       messagebox.show("提示:请输入短信中心服务号码!","短信发送");             this.txtcenter.focus();             return;                    }          else if(this.txtmobile.text.trim()=="")          {             messagebox.show("提示:请输入要发送的手机号!","短信发送");             this.txtmobile.focus();             return;          }          else if(this.txtsms.text.trim()=="")          {             messagebox.show("提示:请输入要发送的短信内容!","短信发送");             this.txtsms.focus();             return;          }                    //将没字转换成16进制                    string strinput=txtsms.text;          string txt="";          string strtemp="";          for (int i = 0; i < strinput.length; i++)          {             string str = "0000" + ((int)strinput).tostring("x");             txt += str.substring(str.length - 4);          }                                                //有效期          string cbo =this.cbobox3.text;          cbo=cbo.substring(0,2);          strinput =txt;          strtemp = strtemp + "08"; //smsc地址信息的长度(91683110301405f0共八位字节)          strtemp = strtemp + "91"; //smsc地址格式(ton/npi)          strtemp = strtemp + strtostr(txtcenter.text.substring(1));         //smsc地址(8613010341500南京的短信服务中心号码)          strtemp = strtemp + "11" ; //基本参数(tp-mti/vfp)( 发送,接收为84)          strtemp = strtemp + "00" ; //mr message reference          strtemp = strtemp + "0d" ; //目标sim卡号码长度(683159717456f4按阿拉伯数字个数计)          strtemp = strtemp + "91" ; //同上          strtemp = strtemp + "68";          strtemp = strtemp + strtostr(txtmobile.text);         //要发送的手机号          strtemp = strtemp + "00"; //普通gsm类型,点到点方式          strtemp = strtemp + "08"; //ucs2编码          strtemp = strtemp + cbo;         //有效期          string smslength = (txt.length / 2).tostring("x");                 strtemp=strtemp + smslength;          strtemp=strtemp + txt;          strtemp = strtemp + (char)26;          // mscomm.output = "at+cmgf=0" + vbcr '以pdu模式发送短信                    mscomm.output = "at+cmgs=" + (15 + txtsms.text.length * 2) + (char)13;                    thread.sleep(2000);          mscomm.output = strtemp;          lblsms.text="正在发送手机短信...";                    int t = environment.tickcount;          int l;          string buffer = "";                              do             {             system.windows.forms.application.doevents();             console.writeline(environment.tickcount - t );             buffer += mscomm.input;             l=buffer.indexof("ok/r/n");          } while(l == -1 && environment.tickcount - t <=5500);                    if (l!=-1)          {             lblsms.text = "短信发送成功!";             }          else          {             lblsms.text = "短信发送失败!";                          }       }       #endregion       #region 输入校验处理=====================================================       private void txtcenter_keypress(object sender, system.windows.forms.keypresseventargs e)       {          if("+0123456789".indexof(e.keychar) == -1 && e.keychar !=(char)8 )          {             e.handled = true ;          }       }       private void txtmobile_keypress(object sender, system.windows.forms.keypresseventargs e)       {          if("0123456789".indexof(e.keychar) == -1 && e.keychar !=(char)8)          {             e.handled = true ;          }                 } #endregion       #region 手机号移位处理===================================================       string strtostr(string txt)       {          txt=txt+"f";          string txt2="";              for (int i = 1; i < txt.length; i=i+2)          {             txt2 = txt2 + txt.substring(i,1);             txt2 = txt2 + txt.substring(i-1,1);                 }          return txt2;       }       #endregion       #region mscomm 返回的信息放入文本框 txtinput 中==========================       private void mscomm_oncomm(object sender, system.eventargs e)       {          txtinput.text=(string)mscomm.input;       }       #endregion       #region 状态栏处理=======================================================       private void statusbar()       {          this.statusbarpanel1.text ="版权所有 2004-2008";          this.statusbarpanel2.text = "设计:super ";          this.statusbarpanel3.text = "网址: http://www.wsoft.cn";       }       #endregion       } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表