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

 

 

Maven tool

You can learn maven as a simple way . The maven it’s a tool by using simplifying the project building.

  1. Maven Provides Using developers way to following :
  • Builds
  • Documentation
  • Reporting
  • Dependencies
  • SCMs
  • Releases
  • Distribution
  • Mailing list
  1. Maven Project configure with .xml file pom.xml(POM-Project object model)
  2. Maven Environment Settings:
  1. POM configuration:
  • Project dependencies
  • Plugins
  • Goals
  • Build profiles
  • Project version
  • Developers
  • Mailing list
  1. Before creating The POM , we should first decide the project group(group Id) , Name(Artifact Id) and its Version .These Attributes are help in uniquely identifying the project repositories.
  2. Example program:

Pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.companyname.project-group</groupId>
   <artifactId>project</artifactId>
   <version>1.0</version>
</project>
  1. The above code, Required Project element and three mandatory fields:groupId, artifactId and version.
  2. Projects notation in repository is groupId:artifactId:version.
  3. An easy way to look at the default configurations of the super POM is by running the following command: mvn help:effective-pom.
  • Maven build life cycle is consists of following phases:
    • Prepare-resource- resource copying can be customized in this phase.
    • Compile- source code compile done in this phase
    • Package- This phase create jar/war packages as mention in the xml file
    • Install- This phase installs the package in local/remote maven repository.
  • Maven has following three standard life cycle.
    • Clean
    • Default(or build)
    • Site

[Note: mvn clean dependency:copy-dependencies package…command]

Clean Life cycle:  

When we run mvn post-clean command, Maven invoke the life cycle phases.

  • Pre-clean
  • Clean
  • Post-clean

Site Life cycle:

Maven Site plugin is generally used to create fresh documentation to create reports, deploy site etc.

Phases:

  • Pre-site
  • Site
  • Post-site
  • Site-deploy

 Build profile:

A Build profile is a set of configuration values which can be used to set or override default values of maven build. Using a build profile, you can use customize build or different environments such as Production v/s deployment environments.

Profile are specified in pom.xml file using its active profiles /profile elements and are triggered in variety of ways. Profile modify the POM at build time, and are used to give parameters different target environment (for example: the path of the database servers in the deployment, testing and production).

Profile Activation project structure (Eclipse format):

eclipe-project-struct

Project Structure in eclipse

  • Maven repositories are three types:
    • Local
    • Central
    • Remote
  • Maven plugins:
    • Plugins are used for:
      • Create jar
      • Create war
      • Compile code files
      • Unit testing of code
      • Create project document
      • Create project report
  • Maven plugin types:
  • Build plugin->they execute during the build and should be configured in the  <build />  element in xml.
  • Reporting plugins->they execute the site generation and they should be     configured in the <reporting /> element of the xml.

click here to download as doc