Programming Tutorials

How to Send SMS using Java Program (full code sample included)

By: Emiley J. in Java Tutorials on 2006-12-17  

It is possible to send SMS using a modem attached to a PC without using an API. This can be achieved by communicating with the modem using AT commands.

AT commands are a set of instructions used to control modems and other telecommunication devices. To send an SMS using a modem via AT commands, you need to connect to the modem using a serial port and send AT commands to initiate a connection with the cellular network and send the message.

Here is a simple example of how to send an SMS using a modem connected to a PC via a serial port using AT commands:

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SMS {
    private Enumeration portList;
    private CommPortIdentifier portId;
    private String messageString = "Hello, World!";
    private SerialPort serialPort;
    private OutputStream outputStream;

    public static void main(String[] args) {
        SMS sms = new SMS();
        sms.sendSMS("+1234567890", "Hello, World!");
    }

    public void sendSMS(String phoneNumber, String message) {
        try {
            portList = CommPortIdentifier.getPortIdentifiers();

            while (portList.hasMoreElements()) {
                portId = (CommPortIdentifier) portList.nextElement();
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                    if (portId.getName().equals("COM1")) {
                        serialPort = (SerialPort) portId.open("SMS", 2000);
                        outputStream = serialPort.getOutputStream();
                        serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                        String initCommand = "ATZ\r\n";
                        String connectCommand = "AT+CMGF=1\r\n";
                        String phoneNumberCommand = "AT+CMGS=\"" + phoneNumber + "\"\r\n";

                        outputStream.write(initCommand.getBytes());
                        Thread.sleep(1000);
                        outputStream.write(connectCommand.getBytes());
                        Thread.sleep(1000);
                        outputStream.write(phoneNumberCommand.getBytes());
                        Thread.sleep(1000);
                        outputStream.write(message.getBytes());
                        outputStream.write(26);
                        Thread.sleep(5000);

                        outputStream.close();
                        serialPort.close();
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

This program connects to the serial port COM1 and sends an SMS message to the specified phone number using the specified message string. Note that this program sends the SMS in PDU mode, which is the standard format for SMS messages. You can modify the program to use other AT commands to send SMS messages in other formats.






Add Comment

* Required information
1000

Comments (2)

Avatar
New
Eswaransays...

I am getting javax.comm.NoSuchPortException

at javax.comm.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:105) at javax.comm.CommPortIdentifier.getPortIdentifier(CommPortIdentifier.java:105)

Avatar
New
Normansays...

comm.jar should be placed in:

%JAVA_HOME%/lib

%JAVA_HOME%/jre/lib/ext

win32com.dll should be placed in:

%JAVA_HOME%/bin

%JAVA_HOME%/jre/bin

%windir%System32

javax.comm.properties should be placed in:

%JAVA_HOME%/lib

%JAVA_HOME%/jre/lib

Most Viewed Articles (in Java )

Latest Articles (in Java)