Send SMS Using Java
private String smsGatewayUserId = "";
private String smsGatewayPassword = "";
private String smsGatewayURL = "";
private String urlEncoding = "UTF-8";
private String smsGatewaySID = "";
private static String csvFilePath = "/var/www/";
public String getMessage(String ar[]) {
return null;
}
public String sendSMS(String mobileNo, String msg) {
String responseFromSMSGateway = "";
try {
URL url = new URL(smsGatewayURL);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
if (!conn.getDoOutput()) {
System.err.println("ERROR: Connection problem to SMS Gateway");
return "";
}
OutputStreamWriter wr = new OutputStreamWriter(
conn.getOutputStream());
wr.write(getEncodedData(mobileNo, responseFromSMSGateway));
wr.flush();
BufferedReader rd = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
responseFromSMSGateway += line;
}
wr.close();
rd.close();
} catch (Throwable e) {
e.printStackTrace();
}
return responseFromSMSGateway;
}
/**
* Encode message in proper format and return it.
*
* @param mobileNo
* @param msg
* @return
*/
private String getEncodedData(String mobileNo, String msg) {
String data = "";
try {
data = URLEncoder.encode("user", urlEncoding) + "="
+ URLEncoder.encode(smsGatewayUserId, urlEncoding);
data += "&" + URLEncoder.encode("password", urlEncoding) + "="
+ URLEncoder.encode(smsGatewayPassword, urlEncoding);
data += "&" + URLEncoder.encode("msisdn", urlEncoding) + "="
+ URLEncoder.encode(mobileNo, urlEncoding);
data += "&" + URLEncoder.encode("msg", urlEncoding) + "="
+ URLEncoder.encode(msg, urlEncoding);
data += "&" + URLEncoder.encode("sid", urlEncoding) + "="
+ URLEncoder.encode(smsGatewaySID, urlEncoding);
data += "&" + URLEncoder.encode("fl", urlEncoding) + "="
+ URLEncoder.encode("0", urlEncoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return data;
}
/**
* Process CSV file
* @param file
*/
private void processFile(File file){
BufferedReader csvFileReader = null;
try {
csvFileReader = new BufferedReader(new FileReader(file));
String lineToRead = null;
String messageId="";
int lineNumber = 1;
while ( null != (lineToRead = csvFileReader.readLine()) ) {
try {
String colums[] = lineToRead.split(",");
if (colums.length == 4) {
messageId=sendSMS(colums[0], getMessage(colums));
System.out.println("RESPONSE: "+ messageId);
} else {
System.err.println("Error: line no " + lineNumber + " does not contains valid data.Skipping this line.");
}
} catch (Throwable e) {
e.printStackTrace();
}
lineNumber++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
csvFileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* #################### Main Method #################
* @param args
*/
public static void main(String[] args) {
SendSMS sendSMS = new SendSMS();
File file = new File(csvFilePath);
if (file.exists()) {
System.out.println("#################### Processing started #################");
sendSMS.processFile(file);
System.out.println("################## DONE ##########");
} else {
System.err.println("Error: File does not exist on its location.="+ csvFilePath);
}
}
}