How to send SMS using Twilio API in Java?

I came across a question on StackOverflow.com yesterday. As, I have used Twilio API to send SMS and do Phone Call many times. I signed up to help the OP.

If you wanna compile following code in your local box, then you need to have JSON.jar and commons-codec-1.8.jar JAR files. Download File Archive

I wrote this class to generalize any Web Related Helper Methods.

To output property, I used Bootstrap 2.3.2 in following JSP.

If you get all things working, the output would look like this.

TwilioMessageExample

7 thoughts on “How to send SMS using Twilio API in Java?

  1. Where to get the json.jar
    I get the following error

    Exception in thread “main” java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonParseException
    at com.twilio.sdk.TwilioRestResponse.getParser(TwilioRestResponse.java:225)
    at com.twilio.sdk.TwilioRestResponse.toMap(TwilioRestResponse.java:243)
    at com.twilio.sdk.resource.list.SmsList.create(SmsList.java:70)
    at Example.main(Example.java:25)
    Caused by: java.lang.ClassNotFoundException: org.codehaus.jackson.JsonParseException
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    … 4 more

  2. Well, I guess! You are using some other package! com.twilio.sdk.* I did not use any SDK to call API in my example.

    If you see stacktrace, you are getting error Class not found exception for org.codehaus.jackson.JsonParseException!

    You should get latest Jackson JSON Parser from http://wiki.fasterxml.com/JacksonDownload

  3. This is very good!
    How to send bulk SMS from this API, I have to send SMS for all new customers who are registering into my application so I have stored their contact numbers in the database and from there I need to fire bulk SMS on various occasions to all of them like if there is a new offer or something like that through my Java code. Is that possible with this API? I do not need UI that makes the user type their own message and it should be automated for 1000s of users. I have done the e-mail part but stuck with the SMS.

  4. Hello Praveen,

    You should read following link to send Bulk SMS via Twilio API.

    https://www.twilio.com/help/faq/sms/what-kind-of-sms-messages-are-not-allowed-to-be-sent-using-twilio

    Best,
    Gaurang

  5. how to send sms in my website using bulk sms

  6. Thanks. The post is still relevant to beginners like me.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.