当前位置:早雪网网络学院编程文档SMS → 用C#设计在局域网发送短信的程序

用C#设计在局域网发送短信的程序

减小字体 增大字体 作者:不详  来源:不详  发布时间:2005-9-14 15:24:33

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

  st

[1] [2]  下一页

[数据载入中...] [返回上一页] [打 印]

文章评论评论内容只代表网友观点,与本站立场无关!

  评论人:fd   打分:85 分  发表时间:2008-10-7 17:40:09
· [linkhttp://www.gulouhospital.com/changshi/2005/7-14/92040.h...
  评论人:fdsa   打分:85 分  发表时间:2008-6-11 22:28:22
· 上海森锐公司专业生产<ahref=http://www.srgoogle.cn/>网站优化</a>,客户得到广大好评.是...
  评论人:newer   打分:100 分  发表时间:2007-11-16 22:03:15
· 新来的报告一下哈哈 
  评论人:精密铸造   打分:100 分  发表时间:2007-11-10 21:33:01
· 采用全硅溶胶脱蜡<ahref=http://www.zhuzaocasting.com>精密铸造</a>工艺生产出口不锈...
  评论人:昆明旅游   打分:100 分  发表时间:2007-11-4 14:56:53
· <ahref=http://www.uyn580.com>昆明旅游</a>资讯网提供各种旅游服务,云南旅游,云南旅游线路...