程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 用C#編寫發手機中文短信息

用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;

//目錄對象  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved