Sending sms to the mobile Using Java code

 Sending sms by using simple Java code

Hi Guys ,This simple java code working as a message sending to the mobile .search for easy to develop the application .

Required jars:

  1. activation-1.1.jar
  2. async-http-client-1.8.14.jar
  3. commons-codec-1.7.jar
  4. commons-io-1.4.jar
  5. commons-lang3-3.0.jar
  6. httpclient-4.5.2.jar
  7. httpcore-4.4.4.jar
  8. jackson-all-1.9.0.jar
  9. jackson-annotations-2.4.3.jar
  10. jackson-core-2.4.3.jar
  11. jackson-core-asl-1.7.1.jar
  12. jackson-databind-2.4.3.jar
  13. jaxb-api-2.2.jar
  14. standard.jar
  15. org-apache-commons-logging.jar
  16. twilio-java-sdk-6.3.0.jar

By sending sms to the mobile using twilio sms free service in my code.Need to create twilio account and generate one contact number it will working certain trailing period only.

In my code using my temporary contact number is: +12015802035 .This number is generated automatically corresponding in my account.

Need to Twilio Account process is:

Exmaple:

Go to url= https://www.twilio.com/  and signup here.

My username: veeru.veeresh0555@gmail.com

Password: xyz@123

Note: this service can provide other technical services also like php,java etc.

Eclipse project Structure:

smspic

HelloTwilio.java:

package com.smsservice;

import java.util.HashMap;

import org.apache.commons.codec.binary.Base64;

import org.apache.http.HttpHost;

import org.apache.http.auth.AuthScope;

import org.apache.http.auth.UsernamePasswordCredentials;

import org.apache.http.client.CredentialsProvider;

import org.apache.http.client.HttpClient;

import org.apache.http.impl.client.BasicCredentialsProvider;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.impl.conn.DefaultProxyRoutePlanner;

import com.twilio.sdk.*;

import com.twilio.sdk.resource.factory.SmsFactory;

import com.twilio.sdk.resource.instance.Account;

public class HelloTwilio {

  public static final String ACCOUNT_SID = "AC56d9cf30ad941b7866822c1077054ba5";
  public static final String AUTH_TOKEN = "d843c4a39749882763f2a12107f7e100";
  public static void main(String[] args) throws TwilioRestException {
  System.out.println(Base64.class.getProtectionDomain().getCodeSource().
                                                                getLocation());
        TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);
        System.out.println("client::" + client);
        client.setHttpClient(getProxyClient());
        Account account = client.getAccount();
        SmsFactory factory = account.getSmsFactory();
        HashMap<String, String> message = new HashMap<>();
        message.put("To", "<To-mobile Number>");
        message.put("From", "+12015802035");//This no.r providing twilio
           message.put("Body", "hii");
           factory.create(message);
     }}

If have some proxy problem we can use below code as a particular method declaring in a class after/before the main method:

private static HttpClient getProxyClient() {

    HttpHost proxy = new HttpHost("10.0.228.80", 9090, "http");

    DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

    credentialsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST,                                                                   AuthScope.ANY_PORT),

    new UsernamePasswordCredentials(ACCOUNT_SID, AUTH_TOKEN));
           CloseableHttpClient httpClient = HttpClients.custom().setRoutePlanner(routePlanner).setDefaultCredentialsProvider(credentialsProvider).build();
           return httpClient;
     }

Explanation:

Account_sid and Auth_Token  credentials are providing twilio account ,It is unique is every user .

 public static final String ACCOUNT_SID = "AC30ad941b1077054ba5";
  public static final String AUTH_TOKEN = "d843c4a39749";

Output: Sending message success

download doc here download sms-doc

 

 

Leave a comment