Java

We offer code samples to make it easy for you to plug in SMS functionality into your applications.
Download our SDK that contains sample projects in many languages or go straight to the language you need with our code samples.

Code Sample

package com.twoSms;

import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class SmsJava
{
    /** Run this Java program to send an SMS message to a cell phone and print out the result */
    public static void main(String[] args)
    {
        try
        {
            // Insert your 2sms account details and test cell phone number here
            String username = “your.address@your.company”;
            String password = “password”;
            String destination = “+10000000000”;
            // Build up XML to send to 2sms (c.f. http://schema.2sms.us/2.0/notes/0410_examples.xml)
            String requestString =
                “<?xml version=’1.0′ encoding=’UTF-8′?>” +
                “<Request xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance’ ” +
                “
xsi:noNamespaceSchemaLocation=’http://schema.2sms.us/2.0/schema/0410_RequestSendMessage.xsd’ ” +
                ”                      Version=’1.0’>” +
                ”     <Identification>” +
                ”         <UserID>” + username + “</UserID>” +
                ”         <Password>” + password + “</Password>” +
                ”     </Identification>” +
                ”     <Service>” +
                ”         <ServiceName>SendMessage</ServiceName>” +
                ”         <ServiceDetail>” +
                ”              <CombiMessage>” +
                ”                  <CombiList>” +
                ”                      <Individual type=’sms’>” + destination + “</Individual>” +
                ”                  </CombiList>” +
                ”                  <Text>Testing combination sends</Text>” +
                ”              </CombiMessage>” +
                ”         </ServiceDetail>” +
                ”     </Service>” +
                “</Request>”;

            // Send your XML in an HTTP POST to the 2sms XML gateway
            URL url = new URL(“http://gateway.2sms.us/xml/xml.jsp”);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(requestString);
            wr.flush();
            wr.close();

            // Put the XML response from 2sms into a DOM
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(conn.getInputStream());

            // Extract the Result or ErrorReason from the DOM
            if (doc.getElementsByTagName(“ErrorCode”).item(0).getTextContent().equals(“00”))
            {
                System.out.println(doc.getElementsByTagName(“Result”).item(0).getTextContent());
            }
            else
            {
                System.out.println(doc.getElementsByTagName(“ErrorReason”).item(0).getTextContent());
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

Scroll to Top