用C#編寫發手機中文簡訊息Windows服務

來源:互聯網
上載者:User
林敏 未知 2002-05-26
最近在電腦城上買了一根NOKIA3210的資料線,玩了幾天改LOGO、改鈴聲後也將資料線扔在一邊。直到前幾天在Http://oxygensoftware.com上看到有發手機簡訊息的二次開發控制項,才想起多日不用的資料線,而且最近在學C#,覺得用C#做個發簡訊息的程式也不錯,經過多天的測試,終於實現用電腦+資料線+手機的模式,實現在單位的區域網路平台上傳送簡訊息了。 由於單位使用到發手機簡訊息的地方有很多,可能是從網頁、可能是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:\sms\data\filetosend有檔案時,先關閉時鐘,將其發送出去,並刪除掉檔案再啟動時鐘 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命令列,進行項目目錄的\bin\debug目錄下,執行“installutil SmsServer”,如果找不到installutil程式,就先Path一下。這時,在管理工具的“服務”下可以找到“SmsServer”服務了。啟動該服務。這裡預設源為目錄F:\Sms\Data\FileToSend,如果這個目錄有.SMS檔案,就讀取其第一行為發送的手機號碼,第二行到文本結束為簡訊息內容,然後傳送簡訊息,再將文本備份到F:\Sms\Data\HadBeenSend\。 讓我們再回頭看一下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。
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.