The easiest way to send an SMS is by using the SmsSender
class. It contains factory methods
like getClickatellSender
and getGsmSender
that creates a connection towards
the sms gateway. It also contains some methods like sendTextSms
and
sendWapSiPush
that makes it easy to send these types of messages.
Example: You want to send a simple text message using the clickatell sms gateway.
// Send SMS with clickatell SmsSender smsSender = SmsSender.getClickatellSender("username", "password", "apiid"); // The message that you want to send. String msg = "A sample SMS."; // International number to reciever without leading "+" String reciever = "461234"; // Number of sender (not supported on all transports) String sender = "461235"; smsSender.connect(); String msgids[] = smsSender.sendTextSms(msg, reciever, sender); smsSender.disconnect();
The other way to send a message is by first loading a transport using the
SmsTransportManager
and then create a message using one of the
SmsMessage
classes.
Example: You want to send a simple text message using the clickatell sms gateway.
// The username, password and apiid is sent to the clickatell transport // in a Properties Properties props = new Properties(); props.setProperty("smsj.clickatell.username", theUsername); props.setProperty("smsj.clickatell.password", thePassword); props.setProperty("smsj.clickatell.apiid", theApiId); // Load the clickatell transport SmsTransport transport = SmsTransportManager.getTransport("org.marre.sms.transport.clickatell.ClickatellTransport", theProps); // Connect to clickatell transport.connect(); // Create the sms message SmsTextMessage textMessage = new SmsTextMessage("A sample SMS."); // Send the sms to "461234" from "461235" transport.send(textMessage, new SmsAddress("461234"), new SmsAddress("461235")); // Disconnect from clickatell transport.disconnect();