Program | design because there are a lot of places in your unit where you send SMS messages, you might be from a Web page, a form in Outlook, or a system that may be a host of a non-Windows operating system, so after thinking about it, think the best solution is to use Windows "services", Periodically read the appropriate information from a fixed-format text file in a directory and send it out. Other clients simply write text messages to the directory. Let's start with the idea. 2fv\d? SB
Rmiw%d4|*z
First of all, the development platform:M=6xyu
,!gsi_6|w
Windows Advance server operating system_[g:i*)
Visual Studio. Net| jqzs.p/f
Oxygen Sms ActiveX Control V2.3 (Share Ware)? F<yuet
Nokia 3210 handsets are connected to the COM1 via a data cable. ! Ir~iob
d<~nea0y
Run visual Studio. Net, create a new C # project, select the Windows Server type of project, named "Smsserver". In the Server1 design screen, "ServerName" is named "Smsserver". Click the View Designer button to switch to the design screen, drag the 1: control in the Windows Forms Toolbox, name "Smstimer", and drag a "EventLog" control in the Components Toolbox. Named "EventLog1". Click Add Reference on the Project menu, select COM page, browse to the directory where the Oxygen Sms ActiveX Control V2.3 program is installed, and locate Smscontrol.ocx to add to selected components. W s? {(Zl
Replace the Server1.cs code with thepst?tk^,;
o8_) F COS
Using System; Bc8i!*sn
Using System.Collections; uh1*| Ww^e
Using System.ComponentModel; X_{rp{i
Using System.Data; 9i '/d, y/
Using System.Diagnostics; +zu) z<3c-s
Using System.ServiceProcess; |5-*,ao
Using System.IO; Rmk~/pfu
Using System.Text}jll
Junz/<>qi
Namespace Smsserveri=6q! '
{ BtS Cu;n
public class SmsServer:System.ServiceProcess.ServiceBasePtnf0wi
{ Ivz-/6_ (x
Private System.Timers.Timer Smstimer; 1rr2| O
Private System.Diagnostics.EventLog EventLog1; Mzkzl8bz
Public O2SMSXCONTROL.O2SMSX smsx1;//defines mobile SMS objects-D; J*0 '
F}}gyc
Required designer variable. W D5FV,BL
#;;:~;lxk
Private System.ComponentModel.Container components = null; } wcyfj:9
Public Smsserver ()! M:z<5e "
{ VW ABJA.F
This are required by the windows.forms Component Designer. & EcD
BC}/ONP
InitializeComponent (); #5 & 8) V
Todo:add any initialization on the initcomponent calley,~) ['
} Scpv}6 ' 0
: Spbk@s
The main entry point for the process8[wgsy$hk
static void Main ()Q$^lpd4
{ C/8K7MC ' N
System.serviceprocess.servicebase[] ServicesToRun; s[qz:i)
Sbvuu#-;xz
More than one user Service may run within the same process. To add6=c,d2ilc
Another service to this process, change the "following line" to|_8cytxw
Create a second service object. For example,X3ckx&ns
// _l=+mdeq
ServicesToRun = new system.serviceprocess.servicebase[] {new Service1 (), New MySecondUserService ()}; Cyi7!zgc
// r5/~oad[
@ke [(}b![ L
ServicesToRun = new system.serviceprocess.servicebase[] {new Smsserver ()}; Wx7
3]-{l <ni
System.ServiceProcess.ServiceBase.Run (ServicesToRun); y# $z 4le#
} TM 3-p
qf?:5mg
W[i '-JD
Required to Designer support-do not modify}6h>z P
The contents is with the Code Editor. T6qa6
' Gvv0>aw
Hq p$sa{b
private void InitializeComponent ()+4{mnmob
{ E% u,! pk+
This. Smstimer = new System.Timers.Timer (); . ~! sug0m
This.eventlog1 = new System.Diagnostics.EventLog (); u^ 1l9y
((System.ComponentModel.ISupportInitialize) (This.smstimer)). BeginInit (); V4tvh*jn
((System.ComponentModel.ISupportInitialize) (THIS.EVENTLOG1)). BeginInit (); nnb!8| -
// tl8>sy;
Smstimerd+)}8v9
// >68 6Y9QJ
This. Smstimer.enabled = true; #X) s (|7w
This. smstimer.elapsed + = new System.Timers.ElapsedEventHandler (this.smstimer_elapsed); B pt}!} 8
// N "j=7
Smsserver~w_ait h[
// Uj ' F K
This. ServiceName = Smsserver; F =1[
((System.ComponentModel.ISupportInitialize) (This.smstimer)). EndInit (); ' 7j! S! ' 8
((System.ComponentModel.ISupportInitialize) (THIS.EVENTLOG1)). EndInit (); G ' I & 5dX
} nq\) ' X7n0
Cstp#xem
=Q) T$yg?
Clean up any being used. Z=v-;j@%
S.yvj[e (
8ly5*u/
heekdd5u
protected override void Dispose (bool disposing)s) 6 ' $? Zj
{ ']^ ' q5h ' P
if (disposing)K4o AX
{ 7vvo$ '.
if (components!= null)G{:-w|x2
{ ktewo:,r@
Components. Dispose (); E "1ZD '%y
} D1/5 ' Jza
} J;p]fbf*b
Base. Dispose (disposing); ? H%q<2wvj
} K.! Ov!? Q
3i$? ) 0l<
U ' ~j6p06
The Set things in motion so your service can does its work. [.! \}0z*
Qcef_s*3
"N|:t$-$-
protected override void OnStart (string[) args)) ' EEO
{ v87}p/
Todo:add code here to start your service. MA (a:ry-
Initializes the phone when the service is started. A| "} np{
SmsX1 = new O2smsxcontrol.o2smsxclass (); rmo|& 2x7
Smsx1.connectionmode = 0; Line type cable2j JIOM1
Smsx1.comnumber = 1; Join port is COM 1VT ' [^.R
Smsx1.model = 0; Mobile phone Type 3210It{odl
Smsx1.open (); Connect mobile phone]x+yrs3qqh
Smsx1.setsmscnumber (+8613800754500);//Information Center numberOcuctu2*g
} m{;= VD
!9QIFV}
Rwk98su-t
Stop this service. NMTB ' Rrk
@HA
DR (B \o
Ypnjlrw
protected override void OnStop ()b37v3n&
{ }5u|3|\6
Todo:add code here to perform any tear-down necessary to stop your service. DL Jfb\
Smsx1.close (); Wy12;iucf
} WZH7 (&l96a
z>opl{|.| S
private void Smstimer_elapsed (object sender, System.Timers.ElapsedEventArgs e)O, "mle[
{ o3y0#^
When F:\\sms\\data\\filetosend has a file, first close the clock, send it out, and delete the file and then start the clock\KIX*RQ #
This. Smstimer.enabled =false; 81o^pdk
G a[wo@
directory Objects? k ' Zkv[c
DirectoryInfo cd = new System.IO.DirectoryInfo (f:\\\\sms\\\\data\\\\filetosend); #}hzu Q
Database record Variables# "KTQQH
String Rsid; X (, ' 6+:KD
String Rsphonenum; V~&-p "OA"/
String Rssmstext; [Q #u4Fx
}k 0 8}RV
String strSQL; V}sn}4$l
G8DH; =t
First, enumerate all the current SMS files in the current directory]qa b_
foreach (FileInfo filesend in CD. GetFiles ())7w| Y "' G
{ >1 "ziwp&
Try. =>kw
{ }c8m,5c
Open each file in turn to read the contents of the file< Z/YQJ
FileStream fs = new FileStream (CD. FullName + \\\\ + filesend.name,filemode.open,fileaccess.read); P "DFKC
StreamReader SR; MG) y 3G
sr = new StreamReader (fs,unicodeencoding.getencoding (GB2312)); ' 9wl%4edn
Rsid = Filesend.name. ToString (); K#dizxk ' S
Rsid = Rsid.replace (. SMS,); \fsb0t$[4
Rsid = Rsid.trim (); Ahq? Xe
Rsphonenum = Sr. ReadLine (); G0=\&rv_zk
Rsphonenum = Rsphonenum.trim (); Yfak#prf
if (rsphonenum.length >11)' Zhxs (
Rsphonenum = rsphonenum.substring (0,10); @0;; 5a
Rssmstext = Sr. ReadToEnd (); N ' \9v sr%
Rssmstext = Rssmstext.trim (); Vvpawi>nd
if (rssmstext.length >50)o]e! G (E
Rssmstext.substring (0,49); 7y\toj}
Fs. Close (); Dl:!%_
Sr. Close (); ] ' n:~*
8i7zvu:s9
Send SMS@r=, ' \wk
Smsx1.sendunicodesmsmessage (Rsphonenum.tostring (), rssmstext.tostring (), 6,false,); Pc_j6;nn.
L-*hchs
Backing up and deleting filesK y9g) y
Filesend.copyto (f:\\\\sms\\\\data\\\\hadbeensend\\\\ + Filesend.name, true); GR) c7y ' *8
Filesend.delete (); A6a}4ept
} Tbky Fm
catch (System.Exception E)U;w ' F
{ Ft5}n*c (E6
Error writing log fileIt ' gs=8
Eventlog1.writeentry (E.message.tostring ()); C (3 wn:mg
} Ovlh<;v
} , h5 "Z 2L
Restart ClockRnluh
This. Smstimer.enabled =true; Tg[k j+
} UYWGPZKCR)
} lxx& ^d,]{
} e| @nNk
>jxrlpk
S7i3-xbj
In the Server1.cs switch design screen, under the Properties window click "Add Installer", the system automatically add ProjectInstaller.cs file, click ServiceInstaller1, set "Server Name" set to " Smsserver ", click" ServiceProcessInstaller1 ", set account as" LocalSystem ". , j0\y
]\t "Iy5|m
Select "Generate Smsserver" in the menu "generate" to correct possible errors. For the DOS command line, the project directory of the \\Bin\\Debug directory, the implementation of the "InstallUtil smsserver", if you can not find the InstallUtil program, the first path. At this point, the "smsserver" service can be found under the "services" of the administrative tools. Start the service. Here the default source is directory F:\\sms\\data\\filetosend, if this directory has. Sms files, read the phone number of the first act sent, the second line to the end of the text message content, and then send SMS, and then back up the text to f:\\sms\\data\\hadbeensend\\. Aljo<6 (?
Ib\t.&uc
<script>zmbbs=1;</script>ktz{| Y
$~. <WFL ' P
Let's look back at the code in Server1.cs. First in the command space to add "using System.IO;" Using System.Text "facilitates the processing of files and text objects, when naming classesJ*rqeaq=8
(Esdt7zz
public class SmsServer:System.ServiceProcess.ServiceBase5pp$x):) E
{ rhp]>~
Private System.Timers.Timer Smstimer; sx</t "#x
Private System.Diagnostics.EventLog EventLog1; , (L_ 8
Public O2SMSXCONTROL.O2SMSX smsx1;//defines mobile SMS objects8:-w~swks
...... O.+^+k
a5>s#
References the defined SmsX1 object in the oxygen control, and then initializes the mobile object when the service is started:* Y!Q#JB
Ft5= ' #
protected override void OnStart (string[) args)9o\^h (J
{ @ea!u:?i
Todo:add code here to start your service. empm_|
Initializes the phone when the service is started. L.K Pqij
SmsX1 = new O2smsxcontrol.o2smsxclass (); lx\c*/
Smsx1.connectionmode = 0; Line type cableP ' 2dtF;
Smsx1.comnumber = 1; Join port is COM 1Es8yl
Smsx1.model = 0; Mobile phone Type 32108EX ' V0lee
Smsx1.open (); Connect mobile phoneX ey|8n
Smsx1.setsmscnumber (+8613800754500);//Information Center number,) n-5g
} @ (0ix;o_l
0 GPVW
G/rxh$uf
One of the things to note is to initialize the information center number, if not initialized, often have the situation. Then when the clock triggers ya notice to turn off the clock first, and then enumerate the current directory. SMS files, send them out, and then turn the clock on, while reading the file, note the encoding of the file "Sr=new StreamReader (fs,unicodeencoding.getencoding (GB2312));" The use of GB2312 encoded reading will not read out garbled, and finally send information, "Smsx1.sendunicodesmsmessage (Rsphonenum.tostring (), rssmstext.tostring (), 6,false ,); "The meaning of each of these parameters can refer to the help of oxygen." Finally, the Message object "Smsx1.close () is released when the service is stopped" and if an error occurs, the Write Error service log file "Eventlog1.writeentry (e.message.tostring ());" This way, you can see the error information in the Windows Event Viewer. ? 2=@w "D/nj
XU B#~swa
But here is a little regret, the message sent through the OCX control in front of a string of the Web site in English, but the registration version will not have this string of words. H\.9]t:hy
<script>zmbbs=1;</script>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.