How to send SMS with java code using SMS getway?
Required Jars:
commons-codec-1.3
commons-httpclient-3.0.1
commons-logging-1.0.4
Code:
package com.es.sms.one;
//http://www.txtlocal.co.uk/api/
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Class to help send SMS messages
*
* @author Pankil Patel
*/
public class SmsHelper {
private static final Log logger = LogFactory.getLog(SmsHelper.class);
private static final String PARAM_USER = “uname”;
private static final String PARAM_PASSWORD = “pword”;
private static final String PARAM_MESSAGE = “message”;
private static final String PARAM_SENDER = “from”;
private static final String PARAM_PHONE_NO = “selectednums”;
private static final String PARAM_INFO = “info”;
/**
* Send an SMS message via the gateway. Returns whether or not the message
* could be successfully sent.
*
* @param sender
* @param phoneNumber
* @param smsText
* @return
*/
public static boolean sendSMS_Live(final String sender,
final String phoneNumber, final String smsText) {
try {
final HttpClient client = new HttpClient();
final HttpMethod method = new GetMethod(
“https://www.txtlocal.com/sendsmspost.php”);
method.setQueryString(new NameValuePair[] {
new NameValuePair(PARAM_USER,
“email.id@gmail.com”),
new NameValuePair(PARAM_PASSWORD, “PASSWORD”),
new NameValuePair(PARAM_MESSAGE, smsText),
new NameValuePair(PARAM_SENDER, sender),
new NameValuePair(PARAM_PHONE_NO, phoneNumber),
new NameValuePair(PARAM_INFO, “1″), });
final int statusCode = client.executeMethod(method);
logger
.debug(“SmsHelper: QueryString>>> “
+ method.getQueryString());
logger.info(“SmsHelper: Status Text>>>”
+ HttpStatus.getStatusText(statusCode));
return true;
} catch (final Exception e) {
logger.error(“Exception sending SMS: ” + e.toString());
return false;
}
}
}
Main Class to run:
package com.es.sms.one;
//http://www.txtlocal.co.uk/api/
public class Test {
public static void main(String[] args) {
System.out.println(“Test”);
SmsHelper.sendSMS_Live(“Pankil Patel”, “+919725006017″, “Hi………….”);
System.out.println(“Done….”);
}
}