最近在电脑城上买了一根NOKIA的数据线玩了几天改LOGO改铃声后也将数据线扔在一边直到前几天在Http://oxygensoftwarecom上看到有发手机短信息的二次开发控件才想起多日不用的数据线而且最近在学C#觉得用C#做个发短信息的程序也不错经过多天的测试终于实现用电脑+数据线+手机的模式实现在单位的局域网平台上发送短信息了
由于单位使用到发手机短信息的地方有很多可能是从网页可能是OUTLOOK中的窗体也可能是某台非Windows操作系统的主机的某个系统所以经过思考探讨觉得最好的解决方案是采用Windows的服务定时从一个目录中固定格式的文本文件中读取出相应的信息发送出去而其它客户端只需往该目录写入文本信息即可思路定下来后就让我们开始吧!
先交待一下开发平台Windows Advance Server操作系统Visual Studio Net Oxygen Sms ActiveX Control V (Share Ware) Nokia 手机通过数据线接在COM上运行Visual Studio Net新建一个C#的项目选择Windows Server类型的项目命名为SmsServer在Server的设计画面将ServerName命名为SmsServer点击视图设计器按钮切换到设计画面在Windows Forms工具箱中拖一时钟控件命名为SmsTimer在Components工具箱中拖一EventLog控件命名为eventLog在项目菜单中点击添加引用选择COM页浏览到安装Oxygen Sms ActiveX Control V程序的目录找到SMSControlocx添加到选定的组件中
将Servercs代码替换为
using System;
using SystemCollections;
using SystemComponentModel;
using SystemData;
using SystemDiagnostics;
using SystemServiceProcess;
using SystemIO;
using SystemText ;
namespace SmsServer
{
public class SmsServer : SystemServiceProcessServiceBase
{
private SystemTimersTimer SmsTimer;
private SystemDiagnosticsEventLog eventLog;
public OSMSXControlOSMSX SmsX;//定义手机短信对象
/// <summary>
/// Required designer variable
/// </summary>
private SystemComponentModelContainer components = null;
public SmsServer()
{
// This call is required by the WindowsForms Component Designer
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
static void Main()
{
SystemServiceProcessServiceBase[] 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 SystemServiceProcessServiceBase[] {new Service() new MySecondUserService()};
//
ServicesToRun = new SystemServiceProcessServiceBase[] { new SmsServer() };
SystemServiceProcessServiceBaseRun(ServicesToRun);
}
/// <summary>
/// Required method for Designer support do not modify
/// the contents of this method with the code editor
/// </summary>
private void InitializeComponent()
{
thisSmsTimer = new SystemTimersTimer();
thiseventLog = new SystemDiagnosticsEventLog();
((SystemComponentModelISupportInitialize)(thisSmsTimer))BeginInit();
((SystemComponentModelISupportInitialize)(thiseventLog))BeginInit();
//
// SmsTimer
//
thisSmsTimerEnabled = true;
thisSmsTimerElapsed += new SystemTimersElapsedEventHandler(thisSmsTimer_Elapsed);
//
// SmsServer
//
thisServiceName = SmsServer;
((SystemComponentModelISupportInitialize)(thisSmsTimer))EndInit();
((SystemComponentModelISupportInitialize)(thiseventLog))EndInit();
}
/// <summary>
/// Clean up any resources being used
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
componentsDispose();
}
}
baseDispose( 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
//开始服务时初始化手机
SmsX = new OSMSXControlOSMSXClass ();
SmsXConnectionMode = ; //联线类型cable
SmsXComNumber = ; //联接端口为com
SmsXModel = ; //手机类型
SmsXOpen (); //联接手机
SmsXSetSMSCNumber (+);//信息中心号码
}
/// <summary>
/// Stop this service
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any teardown necessary to stop your service
SmsXClose ();
}
private void SmsTimer_Elapsed(object sender SystemTimersElapsedEventArgs e)
{
//当f:\sms\data\filetosend有文件时先关闭时钟将其发送出去并删除掉文件再启动时钟
thisSmsTimerEnabled =false;
//目录对象
DirectoryInfo cd = new SystemIODirectoryInfo(F:\\Sms\\Data\\FileToSend);
//数据库记录变量
string rsId;
string rsPhoneNum;
string rsSmsText;
string StrSql;
//首先在当前目录中列举当前的所有SMS文件
foreach(FileInfo FileSend in cdGetFiles ())
{
try
{
//依次打开每个文件读取文件内容
FileStream fs = new FileStream (cdFullName + \\ + FileSendName FileModeOpenFileAccessRead );
StreamReader sr;
sr = new StreamReader(fsUnicodeEncodingGetEncoding (GB));
rsId = FileSendName ToString ();
rsId = rsIdReplace (sms);
rsId = rsIdTrim ();
rsPhoneNum = srReadLine ();
rsPhoneNum = rsPhoneNumTrim ();
if (rsPhoneNumLength >)
rsPhoneNum = rsPhoneNumSubstring ();
rsSmsText = srReadToEnd();
rsSmsText = rsSmsTextTrim ();
if (rsSmsTextLength >)
rsSmsTextSubstring ();
fsClose ();
srClose ();
//发送短信息
SmsXSendUnicodeSMSMessage (rsPhoneNumToString ()rsSmsTextToString ()false);
//备份并删除文件
FileSendCopyTo (F:\\Sms\\Data\\HadBeenSend\\ + FileSendName true);
FileSendDelete ();
}
catch(SystemException E)
{
//出错写LOG文件
eventLogWriteEntry (EMessageToString ());
}
}
//重新启动时钟
thisSmsTimerEnabled =true;
}
}
}
在 Servercs切换设计画面在属性窗口下点击Add Installer系统自动增加ProjectInstallercs文件点击serviceInstaller设置Server Name设置为SmsServer点击serviceProcessInstaller设置Account为LocalSystem
选择菜单生成中的生成SmsServer改正可能有的错误进行DOS命令行进行项目目录的\bin\debug目录下执行installutil SmsServer如果找不到installutil程序就先Path一下这时在管理工具的服务下可以找到SmsServer服务了启动该服务这里默认源为目录F:\Sms\Data\FileToSend如果这个目录有SMS文件就读取其第一行为发送的手机号码第二行到文本结束为短信息内容然后发送短信息再将文本备份到F:\Sms\Data\HadBeenSend\
让我们再回头看一下Servercs中的代码首先在命令空间要增加using SystemIO; using SystemText ; 方便处理文件及文本对象在命名类时
public class SmsServer : SystemServiceProcessServiceBase
{
private SystemTimersTimer SmsTimer;
private SystemDiagnosticsEventLog eventLog;
public OSMSXControlOSMSX SmsX;//定义手机短信对象
引用Oxygen控件中的定义SmsX对象然后在启动服务时初始化手机对象
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service
//开始服务时初始化手机
SmsX = new OSMSXControlOSMSXClass ();
SmsXConnectionMode = ; //联线类型cable
SmsXComNumber = ; //联接端口为com
SmsXModel = ; //手机类型
SmsXOpen (); //联接手机
SmsXSetSMSCNumber (+);//信息中心号码
}
其中要注意的是要初始化信息中心号码如果不初始化经常有发不去的情况然后当时钟触发时要注意先将时钟关掉再列举当前目录中的SMS文件逐一发送出去再将时钟打开同时在读文件时要注意文件的编码 sr=new StreamReader(fsUnicodeEncodingGetEncoding (GB));采用GB编码读取才不会读出乱码出来最后发送信息即可SmsXSendUnicodeSMSMessage (rsPhoneNumToString ()rsSmsTextToString ()false); 其中各个参数的含义可以参照Oxygen的帮助最后在服务停止时释放短信息对象SmsXClose (); 如果出错则写出错服务LOG文件eventLogWriteEntry (EMessageToString ());这样在Windows的事件查看器就可以看到出错的信息了