分享
 
 
 

用C#编写发手机中文短信息Windows服务

王朝c#·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

最近在电脑城上买了一根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”,如

[1] [2] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有