DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Enhance Your Communication Strategy: Deliver Multimedia Messages With AWS Pinpoint
  • What Is Artificially Inflated Traffic?
  • Revolutionizing Customer Engagement: Unleashing the Magic of Communication APIs
  • Supercharge Your Communication With Twilio and Ballerina

Trending

  • Scalable System Design: Core Concepts for Building Reliable Software
  • Beyond Microservices: The Emerging Post-Monolith Architecture for 2025
  • Overcoming React Development Hurdles: A Guide for Developers
  • Accelerating AI Inference With TensorRT

Sending Simple SMS Using Kannel

By 
Kirti Mandwade user avatar
Kirti Mandwade
·
Jul. 09, 15 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
12.2K Views

Join the DZone community and get the full member experience.

Join For Free

Sending SMS via HTTP GET

Sending SMS via HTTP GET

package com.simpleget;

import java.io.IOException;
import java.net.URI;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class simpleGet {

public static void main(String[] args) {
//Create Http client

CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = null;
CloseableHttpResponse response = null;
HttpEntity entity = null;

try{
//Create GET URL 
URI uri = new URIBuilder()
        .setScheme("http")
        .setHost("localhost:13003")
        .setPath("/cgi-bin/sendsms")
        .setParameter("username", "abc")  // kannel username
        .setParameter("password", "xyz")   // kannel password
        .setParameter("smsc", "send")        // smsc name
        .setParameter("dlr-mask", "31")
        .setParameter("from", "Test")          // sender of message (can be any string or number)
        .setParameter("to", "980010.....")   // destination (must be a mobile number)
        .setParameter("text", "hello sending test message")  // message to send
        .build();

httpGet = new HttpGet(uri);

LOG.info(httpGet.getURI());
httpGet.setHeader("content-type", "text/plain");

response = httpclient.execute(httpGet);

        LOG.info(response.getStatusLine());
    entity = response.getEntity();

    EntityUtils.consume(entity);
} catch (Exception e) {

e.printStackTrace();
} finally {
    try {
response.close();
} catch (IOException e) {

e.printStackTrace();
}
}



    }
}


Sending SMS via HTTP POST

package com.simplepost;


import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.http.Consts;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


public class SimplePost {

public static void main(String[] args) {
String url = "http://localhost:13013/cgi-bin/sendsms";

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;

StringEntity entity = null;
 //For sending SMS using POST method requires whole message to be in xml string format. It has all the parameters that are in get only in xml format.

          String inputXML= "<message><submit><da><number>9780.....</number></da><oa><number>Test Sender</number></oa><ud>Hello +esting</ud><from><username>abc</username><password>xyz</password></from><to>send</to></submit></message>";

// While using HTTP POST you have to set content type to "text/xml" or else it wont work
              InputStream in ;
                  entity = new StringEntity(inputXML, ContentType.create("text/xml", Consts.UTF_8));
                 entity.setChunked(true);
                 try { 

                 httpPost = new HttpPost(url);

                 httpPost.setEntity(entity);
                 response = httpClient.execute(httpPost);

                 System.out.println(response.getStatusLine());
                 in=response.getEntity().getContent();
                 String body = IOUtils.toString(in);
                 System.out.println(body);
                    EntityUtils.consume(entity);

} catch (Exception e) {

e.printStackTrace();
} finally {
    try {
response.close();
} catch (IOException e) {

e.printStackTrace();
}
}

}
}


Kannel is an open source SMS Gateway which is used widely for sending SMS (single or bulk SMS).

You can get most of the information about how to configure and use kannel in the user guide

The code snippet provided above have simple way to send SMS using Kannel via HTTP POST and HTTP GET.

SMS

Opinions expressed by DZone contributors are their own.

Related

  • Enhance Your Communication Strategy: Deliver Multimedia Messages With AWS Pinpoint
  • What Is Artificially Inflated Traffic?
  • Revolutionizing Customer Engagement: Unleashing the Magic of Communication APIs
  • Supercharge Your Communication With Twilio and Ballerina

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!