用C#編寫發手機中文短信息
2024-07-21 02:24:43
供稿:網友
最近在電腦城上買了一根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的“事件查看器”就可以看到出錯的信息了。