In the company project, you need to use the SMS cat to send and receive text messages, and find an open-source jsmsengine on the Internet. Tested jsmsengine
Project requirements can be fulfilled.
The jsmsengine open-source package can use a GSM Modem or a mobile phone with an infrared, Bluetooth, or data cable to connect to a computer and send and receive text messages.
The corresponding class library has been downloaded.
Jsmsengine open-source project Homepage
Http://sourceforge.net/projects/jsmsengine/
Download related libraries
Let's configure the development environment first.
1. Copy comm. jar and jsmsengine. jar to the JRE \ Lib \ ext folder, or add the development kit to the development environment.
2. Copy javax. Comm. properties to the \ JRE \ lib directory.
3. Copy win32com. DLL to the \ JRE \ bin directory.
If you use eclipse for development, check the JRE path in install JRE and use that.
Without development tools, JRE is the java_home path you configured in the system environment.
The above work is necessary and important. Now the environment is ready.
In the Device Manager, check which COM port is occupied by the mobile phone, change the COM port and Bit Rate Parameters in the program, and find a mobile phone with a data cable. It is best not to use a memory card.
Send SMS code:
Import Java. util. export list; import Org. jsmsengine. cincomingmessage; import Org. jsmsengine. cmessage; import Org. jsmsengine. coutgoingmessage; import Org. jsmsengine. cservice; @ suppresswarnings ("unchecked")/***** services for sending and receiving text messages */public class msgservice {Private Static cservice cs = NULL; public static void Init (string port, integer FM) {// specify the port number and frequency. cs = new cservice (port, FM);}/*** send SMS ** @ Param phone * Mobile Phone Number *@ Param info * text message content */public static void sendmsg (string phone, string info) {try {system. out. println ("Sending SMS"); // initialize the connection to CS. initialize (); // sets the cache CS for the jsmsengine service. setcachedir (". \ "); // connect int status = cs. connect (); // The connection is successful (it can be a GSM Modem, a mobile phone with an infrared ray, Bluetooth, or a data cable) if (status = cservice. err_ OK) {CS. setoperationmode (cservice. mode_pdu); CS. setsmscnumber (""); coutgoingmessage MSG = new coutgoingmessage (phone, Info); MSG. setmess Ageencoding (cmessage. message_encoding_unicode); If (CS. sendmessage (MSG) = cservice. err_ OK) {system. Out. println ("sent to" + phone + "successfully! ");} Else {system. Out. println (" failed to send text message to "+ phone +! ");} // Disconnect CS. disconnect ();} else {system. out. println ("failed to connect to the device, exception:" + status) ;}} catch (exception e) {e. printstacktrace () ;}}/***** read text message content */public static void readmsg () {// saved to the SMS collection using list msglist = new using List (); try {system. out. println ("reading text message .... "); CS. initialize (); CS. setcachedir (". \ "); // connect to the device int status = cs. connect (); If (status = cservice. err_ OK) {CS. setoperationmode (cservice. mode_pdu); If (CS. readmessages (msglist, cincomingmessage. class_all) = cservice. err_ OK) for (INT I = 0; I <msglist. size (); I ++) {cincomingmessage MSG = (cincomingmessage) msglist. get (I); system. out. println (MSG); // output text message system. out. println (MSG. gettext (); // Delete the sms cs. deletemessage (I);} // disconnect CS. disconnect ();} elsesystem. out. println ("device Connection Failed:" + status);} catch (exception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {Init ("com4", 9600); sendmsg ("15800000000", "this is a test program "); readmsg ();}}
OK. The test is successful!
Project download