Text message C # code

Source: Internet
Author: User
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. IO;

Namespace smsxx
{
/// <Summary>
/// Summary of form1.
/// </Summary>
Public class form1: system. Windows. Forms. Form
{
Private string MSG = ""; // text message content
Private string [] Nums = {"138 *********", "137 ********"}; // mobile phone number to be sent
Private int [] sendtimes; // record the number of retries
Private axsmsocx. axsms axsms1; // The SMS. ocx control they provide
Private system. Timers. Timer timer1;
Private textwriter Tw = NULL; // log writer
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;

Public form1 ()
{
... // All are generated by default.
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
... // All are generated by default.
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{... // All are generated by default.
}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}

Private void axsms1_oncomm (Object sender, system. eventargs E)
{
Int ret = int32.parse (axsms1.commevent. tostring ());
Switch (RET)
{
Case 4444: addlog ("[Error] serial port open ");
Break;
Case 6666: addlog ("[Error] sending timeout ");
Break;
Case 7777: addlog ("[Error] failed to send ");
Break;
Case 8880: addlog ("[succeeded]" + axsms1.lastsentsms );
Break;
Case 8884: String lastsms = axsms1.lastsentsms;
Addlog ("[failed]" + lastsms); // if the first mobile phone sent here is not returned, it is estimated that it is a bug in their control.
Int I = lastsms. lastindexof ("|") + 1;
If (I> 0)
{

I = int32.parse (lastsms. substring (I ));
If (sendtimes [I] <4)
{
Axsms1.sendsmswithoutret (Nums, MSG, I );
Sendtimes [I] + = 1;
}
}
Break;
Case 8888: addlog ("[succeeded]" + axsms1.newsms );
Break;
Case 2000: addlog ("[failed] No network access ");
Axsms1.refresh ();
Break;
Case 2003: addlog ("[failed] login denied ");
Axsms1.refresh ();
Break;
Case 5555: addlog (axsms1.inputinfo );
Break;
Case 4000: addlog (axsms1.read _ SMS (). tostring ());
Break;

}
}

Private void addlog (string log)
{
Tw. writeline (datetime. Now. tostring ("yyyy-mm-dd hh: mm: SS") + "" + Log );
}

Private void form1_load (Object sender, system. eventargs E)
{
Tw = new streamwriter ("smslogs/" + datetime. Now. tostring ("yyyy-mm-dd") + ". log", true );
Axsms1.endcomm ();
Axsms1.commp ORT = 1;
If (! Axsms1.isopen)
{
Axsms1.initcomm ();
}
If (axsms1.isopen)
{
Axsms1.smsnotsaveinsim ();
}
Else
{
Addlog ("[failed] device first initialization ");
}
Timer1.enabled = true;
}

Private void timerincluelapsed (Object sender, system. Timers. elapsedeventargs E)
{
Timer1.enabled = false;

If (! Axsms1.isopen)
{
Axsms1.initcomm ();

}
If (axsms1.isopen)
{
Streamreader sr = new streamreader ("smsconf", system. Text. encoding. getencoding ("gb18030 "));
MSG = Sr. readtoend ();
Sr. Close ();

Sendtimes = new int [nums. Length];
For (INT I = 0; I <nums. length; I ++)
{
Sendtimes= 1;
}
For (INT I = 0; I <nums. length; I ++)
{
While (axsms1.sendingbusystate ())
{
Application. doevents ();
}
If (axsms1.sendsmswithoutret (Nums, MSG, I ))
{
Addlog ("[succeeded]" + Nums+ "->" + MSG );
}
Else
{
Addlog ("[failed]" + Nums+ "->" + MSG );
}

}
For (INT I = 0; I <1000; I ++)
{
Application. doevents ();
System. Threading. thread. Sleep (50 );
}
Axsms1.endcomm ();
}
Else
{
Addlog ("[failed] Second initialization ");
}

Tw. Close ();

Close ();

}

}
}
 

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.