How to Send Sms in Java

How to send SMS to mobile phone from java program- Using free APIs or WebServices

Using the twilio SMS gateway, you can easily accomplish this.

import com.twilio.sdk.TwilioRestClient;
import com.twilio.sdk.TwilioRestException;
import com.twilio.sdk.resource.factory.MessageFactory;
import com.twilio.sdk.resource.instance.Message;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

public class Example {

// Find your Account Sid and Token at twilio.com/user/account
public static final String ACCOUNT_SID = "AC32a3c49700934481addd5ce1659f04d2";
public static final String AUTH_TOKEN = "{{ auth_token }}";

public static void main(String[] args) throws TwilioRestException {
TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

// Build a filter for the MessageList
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Body", "Abdul please?! I show you"));
params.add(new BasicNameValuePair("To", "+14159352345"));
params.add(new BasicNameValuePair("From", "+14158141829"));

MessageFactory messageFactory = client.getAccount().getMessageFactory();
Message message = messageFactory.create(params);
System.out.println(message.getSid());
}
}

Here is a link to the twilio library: https://www.twilio.com/docs/java/install

How to send SMS using Java

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.

  1. SerialConnection.java (This file is used to connect to your COM port from your java program)

  2. SerialConnectionException.java (This file is for handling serial connection exceptions in your Java program)

  3. SerialParameters.java (This program is used to set your COM port properties for connecting to your com port from your java program)

  4. Sender.java (This is the program that implements runnable and sends SMS using the serial connection)

  5. 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;

}
}

Java send and receive SMS. Free SMS gateway?

FYI Simplewire is now OpenMarket.com/MXTelecom.com

There are a couple of free SMS gateways but they all attach a SMS ad in your message to pay for the cost. ZeepMobile is the one I hear about the most. As for paying there are a couple of solution but this all depends on your needs.

Two way communication would require the end user to subscribe to your service. There are a few ways to approach this:

Short Code: you could get your own (www.openmarket.com) or share with others (www.clickatell.com)
You could use a new service www.twilio.com looks to be good but haven't tested it yet.

If one way communication is all you need you could so something like email to gateway sms but then you would need to know the carrier the end user is on.

How to send and receive SMS in java?

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.

Calling HTTP API Using Java to send SMS

And what produces your attempt? An error? Or it compiles well but does'nt do anything? Please share any output too

Well you put "xxxxxxxxx" in args because you don't want us to see the real adress or because you are just copy pasting the example?

Otherwise I'd suggest you to take a look here
I am trying http connection over android

Send unicode sms with java

I've an example project of GSM device communication in Java which I run in a raspberry attached to a SIMCom800L module. It uses jSerialComm for serial communications which works nice with no configuration at all. Please take a look to my repo: https://github.com/pepevalbe/sms-gateway

To send Unicode you need to configure your GSM device Character Set. Check possible sets with this command:

AT+CSCS=?
+CSCS: ("GSM","UCS2","IRA","HEX")

If the answer contains "HEX" or "UCS2", Unicode seems to be supported. I tried with UCS2 and it is working nice. Just change the Character Set with: AT+CSCS="UCS2" This will only affect to sms, not regular commands.

Now your device will properly recognize the unicode strings you send from Java. Don't forget to set the sms text mode as well: AT+CMGF=1

I give you a full example based on my repo:

//SIMCom serial port configuration: 115200 bps, 8 bit data, no parity, 1 bit stop, no data stream control
SerialPort serialPort = SerialPort.getCommPort("serial0");
serialPort.setComPortParameters(115200, 8, ONE_STOP_BIT, NO_PARITY);
serialPort.setFlowControl(FLOW_CONTROL_DISABLED);
serialPort.setComPortTimeouts(TIMEOUT_READ_BLOCKING, 3000, 0);
serialPort.openPort();

// Set sms text mode
String textModeCommand = "AT+CMGF=1\r";
serialPort.writeBytes(textModeCommand.getBytes(StandardCharsets.UTF_8), textModeCommand.length());

// Set UCS2 charset
String characterSetCommand = "AT+CSCS=\"UCS2\"\r";
serialPort.writeBytes(characterSetCommand.getBytes(StandardCharsets.UTF_8), characterSetCommand.length());

// Send sms command
String sendTextCommand1 = "AT+CMGS=\"" + number + "\"\n";
String sendTextCommand2 = text + (char) 26 + "\r";
serialPort.writeBytes(sendTextCommand1.getBytes(StandardCharsets.UTF_8), sendTextCommand1.length());
serialPort.writeBytes(sendTextCommand2.getBytes(StandardCharsets.UTF_16), sendTextCommand2.length());

Notice that in the send sms command I encoded the text part using UTF_16 charset, not UTF_8



Related Topics



Leave a reply



Submit