最近在电脑城上买了一根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。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器