Java Code Sample

HTTP API Code Sample – Java

The code sample below shows you how to send SMS messages quickly using a simple Java snippet based on the HTTP API. The script sends a single 1-way SMS message that will appear to come from "XYZCorp".

Send a 1-Way SMS Using Java


public class SMSSender {

   * Send a 1-way SMS from "XYZCorp"
   * @param String[] Command line arguments
  public static void main(String[] args) {
    // Declare the security credentials to use
    String username = "my_username";
    String password = "SecrEt12345";

    // Set the attributes of the message to send
    String message  = "Hello World";
    String type     = "1-way";
    String senderid = "XYZCorp";
    String to       = "61400000000";

    try {

      // Build URL encoded query string
      String encoding = "UTF-8";
      String queryString = "username=" + URLEncoder.encode(username, encoding)
        + "&password=" + URLEncoder.encode(password, encoding)
        + "&message=" + URLEncoder.encode(message, encoding)
        + "&senderid=" + URLEncoder.encode(senderid, encoding)
        + "&to=" + URLEncoder.encode(to, encoding)
        + "&type=" + URLEncoder.encode(type, encoding);

      // Send request to the API servers over HTTPS
      URL url = new URL("");
      URLConnection conn = url.openConnection();
      OutputStreamWriter wr =
        new OutputStreamWriter(conn.getOutputStream());

      // The response from the gateway is going to look like this:
      // id: a4c5ad77ad6faf5aa55f66a
      // In the event of an error, it will look like this:
      // err: invalid login credentials
      BufferedReader rd = new BufferedReader(
        new InputStreamReader(conn.getInputStream()));
      String result = rd.readLine();

      if(result == null) {
        System.out.println("No response received");
      else if(result.startsWith("id:")) {
        System.out.println("Message sent successfully");
      else {
        System.out.println("Error - " + result);
    catch (Exception e) {
      System.out.println("Error - " + e);

The full HTTP/S API documentation details all available features of directSMS’ Bulk SMS Gateway. For additional help or if you have any questions, please contact our helpful support team.