用C#编写发手机中文短信息Windows服务
点击次数:24 次 发布日期:2008-11-26 15:55:41 作者:源代码网
|
源代码网推荐 由于单位使用到发手机短信息的地方有很多,可能是从网页、可能是OUTLOOK中的窗体、也可能是某台非Windows操作系统的主机的某个系统,所以经过思考探讨,觉得最好的解决方案是采用Windows的“服务”,定时从一个目录中固定格式的文本文件中读取出相应的信息,发送出去。而其它客户端只需往该目录写入文本信息即可。思路定下来后就让我们开始吧! 源代码网推荐 先交待一下开发平台:Windows 2000 Advance Server操作系统、Visual Studio .Net 、Oxygen Sms ActiveX Control V2.3 (Share Ware)、 Nokia 3210手机通过数据线接在COM1上。运行Visual Studio .Net,新建一个C#的项目,选择“Windows Server”类型的项目,命名为“SmsServer”。在Server1的设计画面,将“ServerName”命名为“SmsServer”。点击“视图设计器按钮”切换到设计画面,在“Windows Forms”工具箱中拖一时钟控件,命名为“SmsTimer”,在“Components”工具箱中拖一“EventLog”控件。命名为“eventLog1”。在“项目”菜单中点击“添加引用”,选择“COM”页,浏览到安装Oxygen Sms ActiveX Control V2.3程序的目录,找到SMSControl.ocx添加到“选定的组件”中。 源代码网推荐 将Server1.cs代码替换为 源代码网推荐 源代码网推荐 using System; 源代码网推荐 using System.Collections; 源代码网推荐 using System.ComponentModel; 源代码网推荐 using System.Data; 源代码网推荐 using System.Diagnostics; 源代码网推荐 using System.ServiceProcess; 源代码网推荐 using System.IO; 源代码网推荐 using System.Text ; 源代码网推荐 源代码网推荐 namespace SmsServer 源代码网推荐 { 源代码网推荐 public class SmsServer : System.ServiceProcess.ServiceBase 源代码网推荐 { 源代码网推荐 private System.Timers.Timer SmsTimer; 源代码网推荐 private System.Diagnostics.EventLog eventLog1; 源代码网推荐 public O2SMSXControl.O2SMSX SmsX1;//定义手机短信对象 源代码网推荐 源代码网推荐 /// <summary> 源代码网推荐 /// Required designer variable. 源代码网推荐 /// </summary> 源代码网推荐 private System.ComponentModel.Container components = null; 源代码网推荐 源代码网推荐 public SmsServer() 源代码网推荐 { 源代码网推荐 // This call is required by the Windows.Forms Component Designer. 源代码网推荐 InitializeComponent(); 源代码网推荐 源代码网推荐 // TODO: Add any initialization after the InitComponent call 源代码网推荐 } 源代码网推荐 源代码网推荐 // The main entry point for the process 源代码网推荐 static void Main() 源代码网推荐 { 源代码网推荐 System.ServiceProcess.ServiceBase[] ServicesToRun; 源代码网推荐 源代码网推荐 // More than one user Service may run within the same process. To add 源代码网推荐 // another service to this process, change the following line to 源代码网推荐 // create a second service object. For example, 源代码网推荐 // 源代码网推荐 // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()}; 源代码网推荐 // 源代码网推荐 ServicesToRun = new System.ServiceProcess.ServiceBase[] { new SmsServer() }; 源代码网推荐 源代码网推荐 System.ServiceProcess.ServiceBase.Run(ServicesToRun); 源代码网推荐 } 源代码网推荐 源代码网推荐 /// <summary> 源代码网推荐 /// Required method for Designer support - do not modify 源代码网推荐 /// the contents of this method with the code editor. 源代码网推荐 /// </summary> 源代码网推荐 private void InitializeComponent() 源代码网推荐 { 源代码网推荐 this.SmsTimer = new System.Timers.Timer(); 源代码网推荐 this.eventLog1 = new System.Diagnostics.EventLog(); 源代码网推荐 ((System.ComponentModel.ISupportInitialize)(this.SmsTimer)).BeginInit(); 源代码网推荐 ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit(); 源代码网推荐 // 源代码网推荐 // SmsTimer 源代码网推荐 // 源代码网推荐 this.SmsTimer.Enabled = true; 源代码网推荐 this.SmsTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.SmsTimer_Elapsed); 源代码网推荐 // 源代码网推荐 // SmsServer 源代码网推荐 // 源代码网推荐 this.ServiceName = "SmsServer"; 源代码网推荐 ((System.ComponentModel.ISupportInitialize)(this.SmsTimer)).EndInit(); 源代码网推荐 ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit(); 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 /// <summary> 源代码网推荐 /// Clean up any resources being used. 源代码网推荐 /// </summary> 源代码网推荐 protected override void Dispose( bool disposing ) 源代码网推荐 { 源代码网推荐 if( disposing ) 源代码网推荐 { 源代码网推荐 if (components != null) 源代码网推荐 { 源代码网推荐 components.Dispose(); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 base.Dispose( disposing ); 源代码网推荐 } 源代码网推荐 源代码网推荐 /// <summary> 源代码网推荐 /// Set things in motion so your service can do its work. 源代码网推荐 /// </summary> 源代码网推荐 protected override void OnStart(string[] args) 源代码网推荐 { 源代码网推荐 // TODO: Add code here to start your service. 源代码网推荐 //开始服务时初始化手机. 源代码网推荐 SmsX1 = new O2SMSXControl.O2SMSXClass (); 源代码网推荐 SmsX1.ConnectionMode = 0; //联线类型cable 源代码网推荐 SmsX1.ComNumber = 1; //联接端口为com 1 源代码网推荐 SmsX1.Model = 0; //手机类型3210 源代码网推荐 SmsX1.Open (); //联接手机 源代码网推荐 SmsX1.SetSMSCNumber ("+8613800754500");//信息中心号码 源代码网推荐 } 源代码网推荐 源代码网推荐 /// <summary> 源代码网推荐 /// Stop this service. 源代码网推荐 /// </summary> 源代码网推荐 protected override void OnStop() 源代码网推荐 { 源代码网推荐 // TODO: Add code here to perform any tear-down necessary to stop your service. 源代码网推荐 SmsX1.Close (); 源代码网推荐 } 源代码网推荐 源代码网推荐 private void SmsTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 源代码网推荐 { 源代码网推荐 //当f:smsdatafiletosend有文件时,先关闭时钟,将其发送出去,并删除掉文件再启动时钟 源代码网推荐 this.SmsTimer.Enabled =false; 源代码网推荐 源代码网推荐 //目录对象 源代码网推荐 DirectoryInfo cd = new System.IO.DirectoryInfo("F:\Sms\Data\FileToSend"); 源代码网推荐 //数据库记录变量 源代码网推荐 string rsId; 源代码网推荐 string rsPhoneNum; 源代码网推荐 string rsSmsText; 源代码网推荐 源代码网推荐 string StrSql; 源代码网推荐 源代码网推荐 //首先,在当前目录中列举当前的所有SMS文件 源代码网推荐 foreach(FileInfo FileSend in cd.GetFiles ()) 源代码网推荐 { 源代码网推荐 try 源代码网推荐 { 源代码网推荐 //依次打开每个文件读取文件内容 源代码网推荐 FileStream fs = new FileStream (cd.FullName + "\" + FileSend.Name ,FileMode.Open,FileAccess.Read ); 源代码网推荐 StreamReader sr; 源代码网推荐 sr = new StreamReader(fs,UnicodeEncoding.GetEncoding ("GB2312")); 源代码网推荐 rsId = FileSend.Name .ToString (); 源代码网推荐 rsId = rsId.Replace (".sms",""); 源代码网推荐 rsId = rsId.Trim (); 源代码网推荐 rsPhoneNum = sr.ReadLine (); 源代码网推荐 rsPhoneNum = rsPhoneNum.Trim (); 源代码网推荐 if (rsPhoneNum.Length >11) 源代码网推荐 rsPhoneNum = rsPhoneNum.Substring (0,10); 源代码网推荐 rsSmsText = sr.ReadToEnd(); 源代码网推荐 rsSmsText = rsSmsText.Trim (); 源代码网推荐 if (rsSmsText.Length >50) 源代码网推荐 rsSmsText.Substring (0,49); 源代码网推荐 fs.Close (); 源代码网推荐 sr.Close (); 源代码网推荐 源代码网推荐 //发送短信息 源代码网推荐 SmsX1.SendUnicodeSMSMessage (rsPhoneNum.ToString (),rsSmsText.ToString (),6,false,""); 源代码网推荐 源代码网推荐 //备份并删除文件 源代码网推荐 FileSend.CopyTo ("F:\Sms\Data\HadBeenSend\" + FileSend.Name ,true); 源代码网推荐 FileSend.Delete (); 源代码网推荐 } 源代码网推荐 catch(System.Exception E) 源代码网推荐 { 源代码网推荐 //出错写LOG文件 源代码网推荐 eventLog1.WriteEntry (E.Message.ToString ()); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 //重新启动时钟 源代码网推荐 this.SmsTimer.Enabled =true; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 在 Server1.cs切换设计画面,在属性窗口下点击“Add Installer”,系统自动增加ProjectInstaller.cs文件,点击serviceInstaller1,设置“Server Name”设置为“SmsServer”,点击“serviceProcessInstaller1”,设置Account为“LocalSystem”。 源代码网推荐 选择菜单“生成”中的“生成SmsServer”,改正可能有的错误。进行DOS命令行,进行项目目录的indebug目录下,执行“installutil SmsServer”,如果找不到installutil程序,就先Path一下。这时,在管理工具的“服务”下可以找到“SmsServer”服务了。启动该服务。这里默认源为目录F:SmsDataFileToSend,如果这个目录有.SMS文件,就读取其第一行为发送的手机号码,第二行到文本结束为短信息内容,然后发送短信息,再将文本备份到F:SmsDataHadBeenSend。 源代码网推荐 让我们再回头看一下Server1.cs中的代码。首先在命令空间要增加“using System.IO; using System.Text ; ”方便处理文件及文本对象,在命名类时 源代码网推荐 public class SmsServer : System.ServiceProcess.ServiceBase 源代码网推荐 { 源代码网推荐 private System.Timers.Timer SmsTimer; 源代码网推荐 private System.Diagnostics.EventLog eventLog1; 源代码网推荐 public O2SMSXControl.O2SMSX SmsX1;//定义手机短信对象 源代码网推荐 ...... 源代码网推荐 引用Oxygen控件中的定义SmsX1对象,然后在启动服务时初始化手机对象 源代码网推荐 protected override void OnStart(string[] args) 源代码网推荐 { 源代码网推荐 // TODO: Add code here to start your service. 源代码网推荐 //开始服务时初始化手机. 源代码网推荐 SmsX1 = new O2SMSXControl.O2SMSXClass (); 源代码网推荐 SmsX1.ConnectionMode = 0; //联线类型cable 源代码网推荐 SmsX1.ComNumber = 1; //联接端口为com 1 源代码网推荐 SmsX1.Model = 0; //手机类型3210 源代码网推荐 SmsX1.Open (); //联接手机 源代码网推荐 SmsX1.SetSMSCNumber ("+8613800754500");//信息中心号码 源代码网推荐 } 源代码网推荐 其中要注意的是要初始化信息中心号码,如果不初始化,经常有发不去的情况。然后当时钟触发时要注意先将时钟关掉,再列举当前目录中的.SMS文件,逐一发送出去,再将时钟打开,同时在读文件时,要注意文件的编码 “sr=new StreamReader(fs,UnicodeEncoding.GetEncoding ("GB2312"));”采用GB2312编码读取才不会读出乱码出来,最后发送信息即可,“SmsX1.SendUnicodeSMSMessage (rsPhoneNum.ToString (),rsSmsText.ToString (),6,false,""); ”其中各个参数的含义可以参照Oxygen的帮助。最后在服务停止时释放短信息对象“SmsX1.Close ();” 如果出错,则写出错服务LOG文件“eventLog1.WriteEntry (E.Message.ToString ());”这样,在Windows的“事件查看器”就可以看到出错的信息了。 源代码网推荐 但是这里有个小小的遗憾,通过OCX控件发出的短信息前面有一串该网站的英文,但是注册版不会有这串字,注册“只需”¥399就可以:(。但总的来说还是不错吧,如果有任何问题,欢迎大家一起讨论,我的邮箱是 linmin@wocall.com。 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
