Implementation of SMS using twilio in java

Posted By : Murari Kumar | 16-May-2016

Twilio Java

I had the requirement of the sending messages on mobile number for the two factor authentication. For this requirement i have used twilio which is given by third party.

Twilio is used for send messages to moblie number. With the twilio developer can easily send and receive messages  programatically.If I want to use twilio then first of all we need to register in twilio site and we get account_sid and Authentication token from twilio.After getting account_sid and authentication token, we need to follows these code :--

public Message getMessage(Long to,String body) throws TwilioRestException{

         String ACCOUNT_SID="xxxxxxxxxxxxxxxxxxxxxxxx";

         String AUTH_TOKEN-"xxxxxxxxxxxxxxxxxxxxxxxx";
        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
         
        Account account = client.getAccount();
 
        MessageFactory messageFactory = account.getMessageFactory();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("To","+91"+8927444816)); // Replace with a valid phone number for your account.
        params.add(new BasicNameValuePair("From", "+16317146088")); // Replace with a valid phone number for your account.
        params.add(new BasicNameValuePair("Body","Body of message"));
        Message sms = messageFactory.create(params);

        return sms;
    }

 

If we are using trial version of the twilio(unpaid service) then we need to register receiver mobile number into twilio site.If you are using paid version then this restriction is not applied.

THANKS

About Author

Author Image
Murari Kumar

Murari is a bright Web developer. He has expertise in Java.

Request for Proposal

Name is required

Comment is required

Sending message..