Sending a text through (or how to do a post to a page)

The java examples they provide require the use of an apache library that has superseded. So I wrote my own using simple java libraries. No extras needed.

Obviously you’ll have to get yourself a txt local free text account before you go ahead….

 * To change this template, choose Tools | Templates
 * and open the template in the editor.

package phj;


 * @author paulc
public class SMSSender {

    public String sendSms(String sToPhoneNo,String sMessage) {

    try {
    // Construct data
    String data = URLEncoder.encode("uname", "UTF-8") + "=" + URLEncoder.encode("txtlocalusername****", "UTF-8");
    data += "&" + URLEncoder.encode("pword", "UTF-8") + "=" + URLEncoder.encode("txtlocalpassword****", "UTF-8");
    data += "&" + URLEncoder.encode("message", "UTF-8") + "=" + URLEncoder.encode(sMessage, "UTF-8");
    data += "&" + URLEncoder.encode("from", "UTF-8") + "=" + URLEncoder.encode("from email address", "UTF-8");
    data += "&" + URLEncoder.encode("selectednums", "UTF-8") + "=" + URLEncoder.encode(sToPhoneNo, "UTF-8");
    data += "&" + URLEncoder.encode("info", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8");
    // Send data
    URL url = new URL("");
    URLConnection conn = url.openConnection();
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    String sResult="";
    while ((line = rd.readLine()) != null) {
        // Process line...
        sResult=sResult+line+" ";
    return sResult;
} catch (Exception e) {
    System.out.println("Error SMS "+e);
    return "Error "+e;


Obviously mine is more complex now (hence not final/static), just replace username, password and from email.



About Paul C

I am a Senior Java programmer/Web Technologies, Play in a band called Mandolin Monday, I have a cat and I live on a boat.

Posted on September 9, 2011, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: