用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就可以了。
转载于:https://www.cnblogs.com/kangtr/archive/2006/01/02/309607.html
用C#设计在局域网发送短信的程序相关推荐
- 支付系统设计四:支付核心设计03-快捷发送短信(失败转代扣)
文章目录 前言 一.背景 1. 应用架构 2. 分层支撑机制 二.银行卡快捷支付 1. 用户操作流程 2. 系统执行流程--正常 2.1 发送短信 2.2 短信确认 3. 系统执行流程--异常 3.1 ...
- Arduino+sim800C家居安防火灾报警 拨打电话 发送短信例程程序
家居安防报警器,参考程序. 火灾报警 涉及用sim800c发短信,拨打电话通知. 接线: Sim800c 3.3V -> Arduino 3.3V Sim800c G ...
- android 发短信意图,安卓实现发送短信小程序代码示例
这篇文章主要介绍了Android开发中实现发送短信的小程序示例,文中还附带了一个监听广播接收者的升级版短信发送例子,需要的朋友可以参考下 上图为代码结构图. 现在我们看下具体的代码. Send.jav ...
- gsm pud模式发送短信_GSM的完整形式是什么?
gsm pud模式发送短信 全球移动通信系统 (Global System for Mobile Communication) GSM is an abbreviation of the Global ...
- php调用nexmo发送短信,在 Laravel 中 “规范” 的开发短信验证码发送功能
Laravel简介 Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的网络APP,而且每行 ...
- 延迟发送:4款定时发送短信的应用程序
发送短信已经成为朋友.家人甚至是同事之间最为常见的通讯方式之一.由于短信具备定时发送及发送报告等功能,使用起来非常灵活.当然,短信的应用程序中常常默认带有发送报告功能,而没有定时发送的功能,若是能具备 ...
- linux系统发送短信,使用Linux发送短信
使用Linux发送短信 发布时间:2007-06-14 22:16:03来源:红联作者:Assurance Linux发展到今天,很多功能已经可以在可视化的界面中来完成.不过在很多情况下,命令还是非常 ...
- 怎么在Ubuntu手机上发送短信及拨打电话
由于一些平台安全性的原因,Ubuntu手机目前暂时没有提供供第三方开发者发送短信及拨打电话的接口,但是在实际的应用中,我们也许会需要用到发送短信息或拨打电话.这个时候我们怎么办呢?我们在前面的文章&q ...
- 传智健康项目中相关知识点介绍(如图片存储,发送短信,定时调度,统计报表...)
1. 图片存储方案 1.1 介绍 在实际开发中,我们会有很多处理不同功能的服务器.例如: 应用服务器:负责部署我们的应用 数据库服务器:运行我们的数据库 文件服务器:负责存储用户上传文件的服务器 分服 ...
- 老瓶装新酒 - C#调用WM手机发送短信(源码)
为什么80%的码农都做不了架构师?>>> 一些系统,需要能够发送短信,量很小,平均每日10条. 运营商平台太贵,白名单很严格,小额只能发省内: 各短信平台有各种限制,大事件前后 ...
最新文章
- mq数据写到oracle,MQ+ORACLE 的问题
- Eclipse导入Spring Boot项目后pom.xml出现红叉的解决办法
- opengl游戏引擎源码_跨平台渲染引擎之路:拨云见日
- js设置奇偶行数样式
- docker privileged作用_Docker 从入门到掉坑
- Dll学习心得(2)
- iOS底层探索之多线程(十二)—GCD源码分析(事件源dispatch_source)
- 小白看完都学会了!Jetpack-MVVM-高频提问和解答,面试建议
- 罗永浩2014 一个理想主义者的创业故事Ⅳ 演讲稿实录
- Point Attention Network for Semantic Segmentation of 3D Point Clouds 论文解析
- UART协议及串口回环
- elasticsearch安装 及 启动异常解决
- Python代码对英语标点后面缺空格的情况自动补空格
- Origin——积分工具
- 人人自媒体的时代,程序员该如何利用好自己的优势?我记住了这些神器...
- JackHttp -- 从原理来理解 HTTP
- 上海这边的租房行情(浦东/张江)
- fedora 下的dos模拟器
- E8.ITSM IT运维服务管理系统
- 爬取雪球股票网站数据
热门文章
- 如何用命令行和carbite c++生成sis文件
- python中delete函数_python中的delete函数是如何使用的,最好带例子,谢谢
- golang快速入门[6.2]-集成开发环境-emacs详解
- Convex Optimization 读书笔记 (1)
- 【linux内核分析与应用-陈莉君】文件系统
- informix数据库大全(持续更新)
- mysql 标识列是什么,mysql中标识列是什么意思有什么用
- 西工大机考(社会学概论)大作业网考
- QWQ氏计算器中文版(v1.1.1)c++
- Unity网格变形插件的简单使用:以curve sculpt layered自由变换修改器为例