Use C # To write short messages for Chinese mobile phone sending in Windows

Source: Internet
Author: User
Lin min Unknown 2002-05-26
Recently I bought a nokia3210 data cable in the Computer City. After playing with the logo for a few days and changing the ringtone, I also threw the data cable aside. It wasn't until a few days ago that I saw a secondary development control for sending text messages on http: // oxygensoftware.com that reminds me of data lines that were not used for multiple days, and recently I was learning C #, I think the program of sending short messages with C # is also good. After several days of testing, I finally realized the mode of using computer + data cable + mobile phone to send short messages on the LAN platform of the Organization. Because the unit uses a lot of text messages to send mobile phone messages, it may be from a webpage, a form in outlook, or a system of a host other than a Windows operating system, therefore, after thinking and discussion, I think the best solution is to use Windows's "service" to regularly read and send the corresponding information from a fixed-format text file in a directory. Other clients only need to write text information to the directory. Let's get started after the idea is fixed! Let's talk about the development platform: Windows 2000 advance server operating system, Visual Studio. NET, oxygen SMS ActiveX Control v2.3 (share ware), and Nokia 3210 mobile phone connected to com1. Run Visual Studio. NET to create a C # project. Select a "Windows Server" project and name it "smsserver ". On the Design Screen of server1, name "servername" as "smsserver ". Click "view designer" to switch to the design screen. Drag a clock control in the "Windows Forms" toolbox, name it "smstimer", and drag an "EventLog" Control in the "components" toolbox. Name it "eventlog1 ". Click "add reference" in the "project" menu, select the "com" Page, browse to the directory where the oxygen Gen SMS ActiveX Control v2.3 program is installed, and find smscontrol. add OCX to "selected components. Replace server1.cs code with 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 ;/ // Define the text message object /// <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. servicep Rocess. 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. COMPO Nentmodel. 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 ();} // <summ Ary> // clean up any resources being used. // </Summary> protected override void dispose (bool 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. // initialize the mobile phone when starting the service. smsx1 = new o2smsxcontrol. o2smsxclass (); smsx1.connectionmode = 0; // connection type cable smsx1.comnumber = 1; // The Connection port is com 1 smsx1.model = 0; // mobile phone Type 321 0 smsx1.open (); // connect to the mobile phone smsx1.setsmscnumber ("+ 8613800754500"); // Information Center number} // <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) {// when F: \ SMS \ data \ filetosend has a file, disable the clock, send it out, delete the file, and start the clock this. Smstimer. enabled = false; // directory object directoryinfo Cd = new system. io. directoryinfo ("f :\\ SMS \ data \ filetosend"); // database record variable string rsid; string rsphonenum; string rssmstext; string strsql; // first, list all the current SMS file foreach (fileinfo filesend in CD. getfiles () {try {// open each file in turn to read the file content 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. cl Ose (); Sr. close (); // send the short message smsx1.sendunicodesmessage (rsphonenum. tostring (), rssmstext. tostring (), 6, false, ""); // back up and delete the file filesend. copyto ("F: \ SMS \ data \ hadbeensend \" + filesend. name, true); filesend. delete ();} catch (system. exception e) {// An error occurred while writing the log file eventlog1.writeentry (E. message. tostring () ;}// restart the clock this. smstimer. enabled = true ;}} on the server1.cs switching Design Screen, click "add installer" in the Properties window. The system automatically Add the projectinstaller. CS file, click serviceinstaller1, set "server name" to "smsserver", click "serviceprocessinstaller1", and set the account to "LocalSystem ". Select "generate smsserver" in the "generate" menu to correct possible errors. Run the doscommand line to run "installutil smsserver" in the \ bin \ DEBUG directory of the project directory. If the installutil program cannot be found, first configure the path. In this case, you can find the "smsserver" service under the "service" of the management tool. Start the service. The default source is the directory F: \ SMS \ data \ filetosend. the SMS file reads the mobile phone number sent by the first act. The second line ends with the text and ends with the short message content. Then, it sends the short message and backs up the text to F: \ SMS \ data \ hadbeensend \. Let's look back at the code in server1.cs. First, add "using system. io; using system. text; "makes it easy to process files and text objects. when naming a class, public class smsserver: system. serviceprocess. servicebase {private system. timers. timer smstimer; private system. diagnostics. eventLog eventlog1; Public o2smsxcontrol. o2smsx smsx1; // define the SMS object ...... reference the definition smsx1 object in the oxygen control, and then initialize the mobile phone object protected override void onstart (string [] ARGs) when starting the Service {// todo: Add code here to start your service. // open Initialize the mobile phone when starting the service. smsx1 = new o2smsxcontrol. o2smsxclass (); smsx1.connectionmode = 0; // connection type cable smsx1.comnumber = 1; // The Connection port is com 1 smsx1.model = 0; // mobile phone type 3210 smsx1.open (); // connect to the mobile phone smsx1.setsmscnumber ("+ 8613800754500"); // Information Center number} note that the information center number should be initialized. If the information center number is not initialized, it is often unavailable. When the clock is triggered, you must first turn off the clock and then list it in the current directory. SMS files are sent one by one, and the clock is opened. When reading files, pay attention to the file encoding "sr = new streamreader (FS, unicodeencoding. getencoding ("gb2312"); "is read using gb2312 encoding, so that garbled characters are not read. Finally, the message can be sent." smsx1.sendunicodesmessage (rsphonenum. tostring (), rssmstext. tostring (), 6, false, ""); "the meaning of each parameter can be referred to the help of oxygen. Finally, release the short message object "smsx1.close ();" when the service is stopped. If an error occurs, write the Service Log File "eventlog1.writeentry (E. message. tostring (); "in this way, the error message is displayed in the event viewer of windows. However, there is a small regret here. The short message sent through the OCX control is preceded by a string of English for the website, but the registered version does not have this string. You only need to register "¥399 :(. But in general it is good, if you have any questions, welcome to discuss, my mailbox is linmin@wocall.com.
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.