How to send and receive SMS in java?

Muhammed Refaat picture Muhammed Refaat · Jun 11, 2013 · Viewed 13.6k times · Source

I want my Java application to send and receive SMS without using any additional hardware devices and it must be free.

I made my search but all i found is titles, i found somethings like SMSLib but at the other hand i didn't find tutorials or books to learn that.

I also found that SMSLib code but didn't understand:

Send Message/SMS Code

package SMSEngine;
import org.smslib.*;
class SendMessage
{
public static void sendMessage(String number, String message)
{ 
CService srv = new CService("COM4",9600,"huawei","E220");
try
{
srv.setSimPin("0000");
srv.setSimPin2("0000");
srv.setSmscNumber("");
srv.connect();
COutgoingMessage msg = new COutgoingMessage(number, message); 
msg.setMessageEncoding(CMessage.MessageEncoding.Enc7Bit);
msg.setStatusReport(true);
msg.setValidityPeriod(8);
srv.sendMessage(msg);
srv.disconnect();
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
}
}

Read Message/SMS Codes

package SMSEngine;
import org.smslib.*;
import java.util.*;
class ReadMessages
{
static CService srv;
public static LinkedList receiveMessage()
{
LinkedList msgList = new LinkedList();
/*
To Check COM port Go in following path in Windows7
Control Panel\Hardware and Sound\Bluetooth and Local COM

*/
srv = new CService("COM4",9600,"huawei","E220");//"COM1", 57600, "Nokia", ""
try
{
srv.setSimPin("0000");
srv.setSimPin2("0000");
srv.connect();
srv.readMessages(msgList, CIncomingMessage.MessageClass.Unread);
srv.disconnect();
return msgList;
}
catch (Exception e)
{
e.printStackTrace();
}
System.exit(0);
return msgList;
}
}

Answer

Mike Dinescu picture Mike Dinescu · Jun 11, 2013

In order to send SMS messages you have two options: either use a gateway modem, or use a bulk service with an online API.

SMSLib is only a library that makes it easier to interface with a gateway (hardware device) or with a bulk SMS provider. Either way, the library by itself is not enough.

The code sample that you provided appears to try to use a gateway connected to a local serial port but since you don't have such a hardware device it's not going to work for you.