Loading...

EBulkSMS XML API


Here is some sample code you may use when sending SMS from your java application to EBulkSMS.

Remember you need to have registered an account and generated your API Key in order to test this service.

Here is the code

import java.net.*;
import java.io.*;

public class PostXml {
    public static void main(String[] args) {
        String[] recipients = {"2348023456789", "2348012345678"};
        String xmlrecipients = "";
        String username = "your_email_address";
        String apikey = "your_apikey";
        String sendername = "SenderName";
        String message = "Enter your SMS message content here.";
        String flash = "0";
        String theoutput = "";
        String randmsgid = Double.toString(Math.random());
        for( int i =0; i < recipients.length; i++ ){
            xmlrecipients += "<gsm><msidn>"+ recipients[i] + "</msidn><msgid>" + randmsgid + "_" + i + "</msgid>" + "</gsm>";
        }
        String xmlrequest =
                "<SMS>\n"
                + "<auth>"
                + "<username>" + username + "</username>\n"
                + "<apikey>" + apikey + "</apikey>\n"
                + "</auth>\n"
                + "<message>"
                + "<sender>" + sendername + "</sender>\n"
                + "<messagetext>" + message + "</messagetext>\n"
                + "<flash>" + flash + "</flash>\n"
                + "</message>\n"
                + "<recipients>\n"
                + xmlrecipients
                + "</recipients>\n"
                + "</SMS>";
        
        String theurl = "https://api.ebulksms.com/sendsms.xml";
        PostXml requester = new PostXml();
        theoutput = requester.postXMLData(xmlrequest, theurl);
        if(theoutput.contains("100")){
            System.out.println("100");
        }
        else{
            System.out.println(theoutput);
        }
    }
    
    public String postXMLData(String xmldata, String urlpath){
        String result = "";
        try {
            URL myurl = new URL(urlpath);
            URLConnection urlconn = myurl.openConnection();

            urlconn.setRequestProperty("Content-Type", "text/xml");
            urlconn.setDoOutput(true);
            urlconn.setDoInput(true);
            urlconn.connect();

            //Create a writer to the url
            PrintWriter pw = new PrintWriter(urlconn.getOutputStream());
            pw.write(xmldata, 0, xmldata.length());
            pw.close();

            //Create a reader for the output of the connection
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
            String line = reader.readLine();
            while (line != null) {
                result = result + line + "\n";
                line = reader.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}