Hi,
i want to send sms to mobile using java application. But i dont have any idea for this procees.
So kindly suggest me any ideas.
regards vijayalakshmi
You can use this free Java sample program to send SMS from your PC using GSM modem connected to your computer to your COM port. You also need to download and install the Java comm api from Sun.
This program needs the following java files to function.
SerialConnection.java (This file is used to connect to your COM port from your java program)
SerialConnectionException.java (This file is for handling serial connection exceptions in your Java program)
SerialParameters.java (This program is used to set your COM port properties for connecting to your com port from your java program)
Sender.java (This is the program that implements runnable and sends SMS using the serial connection)
SMSClient.java (This java class is the main class that can be instantiated in your own java program and called to send SMS. This program in turn will use all the above four files internally to send out your SMS). Download Send SMS Java sample program files
/* * * A free Java sample program * A list of java programs to send SMS using your COM serial connection * and a GSM modem * * @author William Alexander * free for use as long as this comment is included * in the program as it is * * More Free Java programs available for download * at http://www.java-samples.com * * * Note: to use this program you need to download all the 5 java files * mentioned on top * */ public class SMSClient implements Runnable{
public final static int SYNCHRONOUS=0; public final static int ASYNCHRONOUS=1; private Thread myThread=null;
private int mode=-1; private String recipient=null; private String message=null;
public int status=-1; public long messageNo=-1;
public SMSClient(int mode) { this.mode=mode; }
public int sendMessage (String recipient, String message){ this.recipient=recipient; this.message=message; //System.out.println("recipient: " + recipient + " message: " + message); myThread = new Thread(this); myThread.start(); // run(); return status; } public void run(){
Sender aSender = new Sender(recipient,message); try{ //send message aSender.send (); // System.out.println("sending ... "); //in SYNCHRONOUS mode wait for return : 0 for OK, //-2 for timeout, -1 for other errors if (mode==SYNCHRONOUS) { while (aSender.status == -1){ myThread.sleep (1000); } } if (aSender.status == 0) messageNo=aSender.messageNo ; }catch (Exception e){ e.printStackTrace(); } this.status=aSender.status ; aSender=null;
} }
**You can use this free Java sample program to send SMS from your PC using GSM modem connected to your computer to your COM port.
You also need to download and install the Java comm api from Sun.http://www.oracle.com/technetwork/java/index-jsp-141752.html
This program needs the following java files to function.
SMSClient.java (This java class is the main class that can be instantiated in your own java program and called to send SMS. This program in turn will use all the above four files internally to send out your SMS).
SMSClient.java
package sms;
public class SMSClient implements Runnable{
public final static int SYNCHRONOUS=0; public final static int ASYNCHRONOUS=1; private Thread myThread=null;
private int mode=-1; private String recipient=null; private String message=null;
public int status=-1; public long messageNo=-1;
public SMSClient(int mode) { this.mode=mode; }
public int sendMessage (String recipient, String message){ this.recipient=recipient; this.message=message; //System.out.println("recipient: " + recipient + " message: " + message); myThread = new Thread(this); myThread.start(); // run(); return status; } public void run(){
Sender aSender = new Sender(recipient,message); try{ //send message aSender.send ();// System.out.println("sending ... ");//in SYNCHRONOUS mode wait for return : 0 for OK, -2 for timeout, -1 for other errors if (mode==SYNCHRONOUS) { while (aSender.status == -1){ myThread.sleep (1000); } } if (aSender.status == 0) messageNo=aSender.messageNo ; }catch (Exception e){e.printStackTrace();} this.status=aSender.status ; aSender=null;
} }
Sender.java
package sms;
import java.util.Date;
public class Sender implements Runnable {
private static final long STANDARD=500; private static final long LONG=2000; private static final long VERYLONG=20000;
SerialConnection mySerial =null;
static final private char cntrlZ=(char)26; String in, out; Thread aThread=null; private long delay=STANDARD; String recipient=null; String message=null;
private String csca="+ "; // the message center private SerialParameters defaultParameters= new SerialParameters ("COM2",9600,0,0,8,1,0); public int step; public int status=-1; public long messageNo=-1;
public Sender(String recipient, String message){
this.recipient=recipient; this.message=message;
} /**
connect to the port and start the dialogue thread */ public int send () throws Exception{
SerialParameters params = defaultParameters;
mySerial =new SerialConnection (params);
mySerial.openConnection();
aThread=new Thread(this);
aThread.start() ; //log("start");
return 0; }
/**
handle time out */
public void run(){
boolean timeOut=false; long startTime=(new Date()).getTime();
while ((step <7) && (!timeOut)){ // log(""+((new Date()).getTime() - startTime); //check where we are in specified delay timeOut=((new Date()).getTime() - startTime)>delay;
//if atz does not work, type to send cntrlZ and retry, in case a message was stuck if (timeOut && (step==1)) { step=-1; mySerial.send( ""+cntrlZ); }
//read incoming string String result= mySerial.getIncommingString() ;
// log ("<- "+result+"\n--------"); int expectedResult=-1;
try{ //log ("Step:"+step);switch (step){ case 0: mySerial.send("atz"); delay=LONG; startTime=(new Date()).getTime(); break; case 1: delay=STANDARD; mySerial.send("ath0"); startTime=(new Date()).getTime(); break; case 2: expectedResult=result.indexOf("OK"); //log ("received ok ="+expectedResult); if (expectedResult>-1){ mySerial.send("at+cmgf=1"); startTime=(new Date()).getTime(); }else{ step=step-1; } break; case 3: expectedResult=result.indexOf("OK"); // log ("received ok ="+expectedResult); if (expectedResult>-1){ mySerial.send("at+csca=\""+csca+"\""); startTime=(new Date()).getTime(); }else{ step=step-1; } break; case 4: expectedResult=result.indexOf("OK"); // log ("received ok ="+expectedResult); if (expectedResult>-1){ mySerial.send("at+cmgs=\""+recipient+"\""); startTime=(new Date()).getTime(); }else{ step=step-1; } break; case 5: expectedResult=result.indexOf(">"); // log ("received ok ="+expectedResult); if (expectedResult>-1){ mySerial.send(message+cntrlZ); startTime=(new Date()).getTime(); }else{ step=step-1; } delay=VERYLONG;//waitning for message ack break; case 6: expectedResult=result.indexOf("OK"); //read message number if (expectedResult>-1){ int n=result.indexOf("CMGS:"); result=result.substring(n+5); n=result.indexOf("\n"); status=0; messageNo=Long.parseLong(result.substring(0,n).trim() ); log ("sent message no:"+messageNo); }else{ step=step-1; } break; } step=step+1; aThread.sleep(100);}catch (Exception e){ e.printStackTrace(); } } mySerial.closeConnection() ; //if timed out set status if (timeOut ) { status=-2; log("*** time out at step "+step+"***"); }
} /**
> SerialConnection.java
package sms;
import javax.comm.; import java.io.; import java.awt.TextArea; import java.awt.event.*; import java.util.TooManyListenersException;
/** A class that handles the details of a serial connection. Reads from one TextArea and writes to a second TextArea. Holds the state of the connection. */ public class SerialConnection implements SerialPortEventListener, CommPortOwnershipListener { // private SerialDemo parent;
/* private TextArea messageAreaOut; private TextArea messageAreaIn; */
private SerialParameters parameters; private OutputStream os; private InputStream is; private KeyHandler keyHandler; private CommPortIdentifier portId; private SerialPort sPort; private boolean open; private String receptionString=""; public String getIncommingString(){ byte[] bVal= receptionString.getBytes(); receptionString=""; return new String (bVal);
}
/** Creates a SerialConnection object and initilizes variables passed in as params. @param parent A SerialDemo object. @param parameters A SerialParameters object. @param messageAreaOut The TextArea that messages that are to be sent out of the serial port are entered into. @param messageAreaIn The TextArea that messages comming into the serial port are displayed on. */ public SerialConnection(SerialParameters parameters) { this.parameters = parameters; open = false;
}
/** Attempts to open a serial connection and streams using the parameters in the SerialParameters object. If it is unsuccesfull at any step it returns the port to a closed state, throws a SerialConnectionException, and returns.
Gives a timeout of 30 seconds on the portOpen to allow other applications to reliquish the port if have it open and no longer need it. */ public void openConnection() throws SerialConnectionException {
// System.out.println("OK 0 "); // Obtain a CommPortIdentifier object for the port you want to open. try { // System.out.println(parameters.getPortName()); portId = CommPortIdentifier.getPortIdentifier(parameters.getPortName()); } catch (NoSuchPortException e) { System.out.println("Yes the problem is here 1 "); e.printStackTrace(); // throw new SerialConnectionException(e.getMessage()); }catch(Exception e) { // System.out.println("ErrorErrorErrorError"); e.printStackTrace(); } //System.out.println(portId); //System.out.println("OK 1 "); // Open the port represented by the CommPortIdentifier object. Give // the open call a relatively long timeout of 30 seconds to allow // a different application to reliquish the port if the user // wants to. try { sPort = (SerialPort)portId.open("SMSConnector", 30000); } catch (PortInUseException e) {throw new SerialConnectionException(e.getMessage());} //System.out.println("OK 2 "); sPort.sendBreak(1000); // Set the parameters of the connection. If they won't set, close the // port before throwing an exception. try { setConnectionParameters(); } catch (SerialConnectionException e) { sPort.close(); throw e; } // System.out.println("OK 3 "); // Open the input and output streams for the connection. If they won't // open, close the port before throwing an exception. try { os = sPort.getOutputStream(); is = sPort.getInputStream(); } catch (IOException e) { sPort.close(); throw new SerialConnectionException("Error opening i/o streams"); }
//System.out.println("OK 4 "); /* // Create a new KeyHandler to respond to key strokes in the // messageAreaOut. Add the KeyHandler as a keyListener to the // messageAreaOut. keyHandler = new KeyHandler(os); messageAreaOut.addKeyListener(keyHandler); */ // Add this object as an event listener for the serial port. try { sPort.addEventListener(this); } catch (TooManyListenersException e) { sPort.close(); throw new SerialConnectionException("too many listeners added"); } //System.out.println("OK 5 "); // Set notifyOnDataAvailable to true to allow event driven input. sPort.notifyOnDataAvailable(true);
// Set notifyOnBreakInterrup to allow event driven break handling. sPort.notifyOnBreakInterrupt(true); // Set receive timeout to allow breaking out of polling loop during // input handling. try { sPort.enableReceiveTimeout(30); } catch (UnsupportedCommOperationException e) { }
//System.out.println("OK 6 "); // Add ownership listener to allow ownership event handling. portId.addPortOwnershipListener(this);
open = true; } /** Sets the connection parameters to the setting in the parameters object. If set fails return the parameters object to origional settings and throw exception. */ public void setConnectionParameters() throws SerialConnectionException { // Save state of parameters before trying a set. int oldBaudRate = sPort.getBaudRate(); int oldDatabits = sPort.getDataBits(); int oldStopbits = sPort.getStopBits(); int oldParity = sPort.getParity(); int oldFlowControl = sPort.getFlowControlMode(); // Set connection parameters, if set fails return parameters object // to original state. try { sPort.setSerialPortParams(parameters.getBaudRate(), parameters.getDatabits(), parameters.getStopbits(), parameters.getParity()); } catch (UnsupportedCommOperationException e) { parameters.setBaudRate(oldBaudRate); parameters.setDatabits(oldDatabits); parameters.setStopbits(oldStopbits); parameters.setParity(oldParity); throw new SerialConnectionException("Unsupported parameter"); } // Set flow control. try { sPort.setFlowControlMode(parameters.getFlowControlIn() | parameters.getFlowControlOut()); } catch (UnsupportedCommOperationException e) { throw new SerialConnectionException("Unsupported flow control"); } } /** Close the port and clean up associated elements. */ public void closeConnection() { // If port is alread closed just return. if (!open) { return; } // Remove the key listener.
// messageAreaOut.removeKeyListener(keyHandler);
// Check to make sure sPort has reference to avoid a NPE. if (sPort != null) { try { // close the i/o streams. os.close(); is.close(); } catch (IOException e) { System.err.println(e); }// Close the port. sPort.close(); // Remove the ownership listener. portId.removePortOwnershipListener(this);} open = false; } /** Send a one second break signal. */ public void sendBreak() { sPort.sendBreak(1000); } /** Reports the open status of the port. @return true if port is open, false if port is closed. */ public boolean isOpen() { return open; } /** Handles SerialPortEvents. The two types of SerialPortEvents that this program is registered to listen for are DATA_AVAILABLE and BI. During DATA_AVAILABLE the port buffer is read until it is drained, when no more data is availble and 30ms has passed the method returns. When a BI event occurs the words BREAK RECEIVED are written to the messageAreaIn. */ public void serialEvent(SerialPortEvent e) { // Create a StringBuffer and int to receive input data. StringBuffer inputBuffer = new StringBuffer(); int newData = 0; // Determine type of event. switch (e.getEventType()) {// Read data until -1 is returned. If \r is received substitute // \n for correct newline handling. case SerialPortEvent.DATA_AVAILABLE: while (newData != -1) { try { newData = is.read(); if (newData == -1) { break; } if ('\r' == (char)newData) { inputBuffer.append('\n'); } else { inputBuffer.append((char)newData); } } catch (IOException ex) { System.err.println(ex); return; } } // Append received data to messageAreaIn. receptionString=receptionString+ (new String(inputBuffer)); //System.out.print("<-"+receptionString); break; // If break event append BREAK RECEIVED message. case SerialPortEvent.BI: receptionString=receptionString+("\n--- BREAK RECEIVED ---\n");} } /** Handles ownership events. If a PORT_OWNERSHIP_REQUESTED event is received a dialog box is created asking the user if they are willing to give up the port. No action is taken on other types of ownership events. */ public void ownershipChange(int type) { /* if (type == CommPortOwnershipListener.PORT_OWNERSHIP_REQUESTED) { PortRequestedDialog prd = new PortRequestedDialog(parent); } */ } /** A class to handle KeyEvents generated by the messageAreaOut. When a KeyEvent occurs the char that is generated by the event is read, converted to an int and writen to the OutputStream for the port. */ class KeyHandler extends KeyAdapter { OutputStream os; /** Creates the KeyHandler. @param os The OutputStream for the port. */ public KeyHandler(OutputStream os) { super(); this.os = os; } /** Handles the KeyEvent. Gets the char</char> generated by the KeyEvent, converts it to an int, writes it to the OutputStream for the port. */ public void keyTyped(KeyEvent evt) { char newCharacter = evt.getKeyChar(); if ((int)newCharacter==10) newCharacter = '\r'; System.out.println ((int)newCharacter); try { os.write((int)newCharacter); } catch (IOException e) { System.err.println("OutputStream write error: " + e); } } } public void send(String message) { byte[] theBytes= (message+"\n").getBytes(); for (int i=0; i<theBytes.length;i++){char newCharacter = (char)theBytes[i]; if ((int)newCharacter==10) newCharacter = '\r'; try { os.write((int)newCharacter); } catch (IOException e) { System.err.println("OutputStream write error: " + e); } } //System.out.println (">'" +message +"' sent"); }
}
SerialConnectionException.java
package sms;
public class SerialConnectionException extends Exception {
/** * Constructs a SerialConnectionException * with the specified detail message. * * @param s the detail message. */ public SerialConnectionException(String str) { super(str); } /** * Constructs a SerialConnectionException * with no detail message. */ public SerialConnectionException() { super(); }
}
SerialParameters.java
package sms;
import javax.comm.*;
public class SerialParameters {
private String portName; private int baudRate; private int flowControlIn; private int flowControlOut; private int databits; private int stopbits; private int parity; /** Default constructer. Sets parameters to no port, 9600 baud, no flow control, 8 data bits, 1 stop bit, no parity. */ public SerialParameters () { this("", 9600, SerialPort.FLOWCONTROL_NONE, SerialPort.FLOWCONTROL_NONE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE ); } /** Paramaterized constructer. @param portName The name of the port. @param baudRate The baud rate. @param flowControlIn Type of flow control for receiving. @param flowControlOut Type of flow control for sending. @param databits The number of data bits. @param stopbits The number of stop bits. @param parity The type of parity. */ public SerialParameters(String portName, int baudRate, int flowControlIn, int flowControlOut, int databits, int stopbits, int parity) {this.portName = portName; this.baudRate = baudRate; this.flowControlIn = flowControlIn; this.flowControlOut = flowControlOut; this.databits = databits; this.stopbits = stopbits; this.parity = parity;} /** Sets port name. @param portName New port name. */ public void setPortName(String portName) { this.portName = portName; } /** Gets port name. @return Current port name. */ public String getPortName() { return portName; } /** Sets baud rate. @param baudRate New baud rate. */ public void setBaudRate(int baudRate) { this.baudRate = baudRate; } /** Sets baud rate. @param baudRate New baud rate. */ public void setBaudRate(String baudRate) { this.baudRate = Integer.parseInt(baudRate); } /** Gets baud rate as an int. @return Current baud rate. */ public int getBaudRate() { return baudRate; } /** Gets baud rate as a String. @return Current baud rate. */ public String getBaudRateString() { return Integer.toString(baudRate); } /** Sets flow control for reading. @param flowControlIn New flow control for reading type. */ public void setFlowControlIn(int flowControlIn) { this.flowControlIn = flowControlIn; } /** Sets flow control for reading. @param flowControlIn New flow control for reading type. */ public void setFlowControlIn(String flowControlIn) { this.flowControlIn = stringToFlow(flowControlIn); } /** Gets flow control for reading as an int. @return Current flow control type. */ public int getFlowControlIn() { return flowControlIn; } /** Gets flow control for reading as a String. @return Current flow control type. */ public String getFlowControlInString() { return flowToString(flowControlIn); } /** Sets flow control for writing. @param flowControlIn New flow control for writing type. */ public void setFlowControlOut(int flowControlOut) { this.flowControlOut = flowControlOut; } /** Sets flow control for writing. @param flowControlIn New flow control for writing type. */ public void setFlowControlOut(String flowControlOut) { this.flowControlOut = stringToFlow(flowControlOut); } /** Gets flow control for writing as an int. @return Current flow control type. */ public int getFlowControlOut() { return flowControlOut; } /** Gets flow control for writing as a String. @return Current flow control type. */ public String getFlowControlOutString() { return flowToString(flowControlOut); } /** Sets data bits. @param databits New data bits setting. */ public void setDatabits(int databits) { this.databits = databits; } /** Sets data bits. @param databits New data bits setting. */ public void setDatabits(String databits) { if (databits.equals("5")) { this.databits = SerialPort.DATABITS_5; } if (databits.equals("6")) { this.databits = SerialPort.DATABITS_6; } if (databits.equals("7")) { this.databits = SerialPort.DATABITS_7; } if (databits.equals("8")) { this.databits = SerialPort.DATABITS_8; } } /** Gets data bits as an int. @return Current data bits setting. */ public int getDatabits() { return databits; } /** Gets data bits as a String. @return Current data bits setting. */ public String getDatabitsString() { switch(databits) { case SerialPort.DATABITS_5: return "5"; case SerialPort.DATABITS_6: return "6"; case SerialPort.DATABITS_7: return "7"; case SerialPort.DATABITS_8: return "8"; default: return "8"; } } /** Sets stop bits. @param stopbits New stop bits setting. */ public void setStopbits(int stopbits) { this.stopbits = stopbits; } /** Sets stop bits. @param stopbits New stop bits setting. */ public void setStopbits(String stopbits) { if (stopbits.equals("1")) { this.stopbits = SerialPort.STOPBITS_1; } if (stopbits.equals("1.5")) { this.stopbits = SerialPort.STOPBITS_1_5; } if (stopbits.equals("2")) { this.stopbits = SerialPort.STOPBITS_2; } } /** Gets stop bits setting as an int. @return Current stop bits setting. */ public int getStopbits() { return stopbits; } /** Gets stop bits setting as a String. @return Current stop bits setting. */ public String getStopbitsString() { switch(stopbits) { case SerialPort.STOPBITS_1: return "1"; case SerialPort.STOPBITS_1_5: return "1.5"; case SerialPort.STOPBITS_2: return "2"; default: return "1"; } } /** Sets parity setting. @param parity New parity setting. */ public void setParity(int parity) { this.parity = parity; } /** Sets parity setting. @param parity New parity setting. */ public void setParity(String parity) { if (parity.equals("None")) { this.parity = SerialPort.PARITY_NONE; } if (parity.equals("Even")) { this.parity = SerialPort.PARITY_EVEN; } if (parity.equals("Odd")) { this.parity = SerialPort.PARITY_ODD; } } /** Gets parity setting as an int. @return Current parity setting. */ public int getParity() { return parity; } /** Gets parity setting as a String. @return Current parity setting. */ public String getParityString() { switch(parity) { case SerialPort.PARITY_NONE: return "None"; case SerialPort.PARITY_EVEN: return "Even"; case SerialPort.PARITY_ODD: return "Odd"; default: return "None"; } } /** Converts a String describing a flow control type to an int type defined in SerialPort. @param flowControl A string describing a flow control type. @return An int describing a flow control type. */ private int stringToFlow(String flowControl) { if (flowControl.equals("None")) { return SerialPort.FLOWCONTROL_NONE; } if (flowControl.equals("Xon/Xoff Out")) { return SerialPort.FLOWCONTROL_XONXOFF_OUT; } if (flowControl.equals("Xon/Xoff In")) { return SerialPort.FLOWCONTROL_XONXOFF_IN; } if (flowControl.equals("RTS/CTS In")) { return SerialPort.FLOWCONTROL_RTSCTS_IN; } if (flowControl.equals("RTS/CTS Out")) { return SerialPort.FLOWCONTROL_RTSCTS_OUT; } return SerialPort.FLOWCONTROL_NONE; } /** Converts an int describing a flow control type to a String describing a flow control type. @param flowControl An int describing a flow control type. @return A String describing a flow control type. */ String flowToString(int flowControl) { switch(flowControl) { case SerialPort.FLOWCONTROL_NONE: return "None"; case SerialPort.FLOWCONTROL_XONXOFF_OUT: return "Xon/Xoff Out"; case SerialPort.FLOWCONTROL_XONXOFF_IN: return "Xon/Xoff In"; case SerialPort.FLOWCONTROL_RTSCTS_IN: return "RTS/CTS In"; case SerialPort.FLOWCONTROL_RTSCTS_OUT: return "RTS/CTS Out"; default: return "None"; } }
}
Send.java
package sms;
public class Send { public static void main(String[]args) { int mode = 5; SMSClient sc = new SMSClient(mode); sc.sendMessage("+ 00", "Hai"); }
}
To send sms to a mobile in java ,you can use sms gateways.Here you can see the code http://java2carrer.blogspot.in/2013/12/send-sms-from-your-web-application.html
To send sms to a mobile in java ,you can use sms gateways.Here you can see the code http://java2carrer.blogspot.in/2013/12/send-sms-from-your-web-application.html
To send sms to a mobile in java ,you can use sms gateways.Here you can see the code http://java2carrer.blogspot.in/2013/12/send-sms-from-your-web-application.html
Ads