Sending SMS Java

package com.sendmode.sms;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.apache.log4j.Logger;

public class SMSSender {

private static Logger log = Logger.getLogger(SMSSender.class);

/**
*
* @param mobileNumber – must be countrycode plus area code plus number. i.e. 353871234567
* @param message – the message to be sent
* @return
*/
public String sendSms(String mobileNumber, String message) {
try {
// Construct data
// Replace the userId and password with your own
// Note: senderid is what appears as the “from number” when the person receives the text
String params = “username=”
+ URLEncoder.encode(“[userId]”, “UTF-8”);
params += “&password=” + URLEncoder.encode(“[password]”, “UTF-8”);
params += “&numto=” + URLEncoder.encode(mobileNumber, “UTF-8”);
params += “&data1=” + URLEncoder.encode(message, “UTF-8”);

params += “&senderid=” + URLEncoder.encode(“Sendmode”, “UTF-8”);
params += “&type=” + URLEncoder.encode(“sendparam”, “UTF-8”);

// Send data
URL url = new URL(“http://api.sendmode.com/httppost.aspx”);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);

OutputStreamWriter osw = new OutputStreamWriter(
conn.getOutputStream());
//POST the data
osw.write(params);
osw.flush();

// Get the response
BufferedReader br = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String line;
String result = “”;
while ((line = br.readLine()) != null) {
// Process lineā€¦
result = result + line + ” “;
}
// if successful, result will contain the following:
//True
try{
osw.close();
br.close();
}catch(IOException e){
log.error(“A problem occurred when freeing resources : ” + e.getMessage());
}
return result;
} catch (Exception e) {
log.error(“An error occurred sending the SMS Message : ” + e.getMessage());
}
return null;
}
}