Send OTP to mobile using Java with source code

 

How to Send OTP (ONE TIME PASSWORD) to Mobile Using Java?

As we know, In most of the projects we require to generate and send OTP to mobile. So, here we will see how to send OTP using Java.

For generating OTP we will use Random and StringBuilder Class of Java. 

GENERATING OTP USING JAVA (SOURCE CODE):-  [CLICK HERE]

But, For sending the OTP to mobile we have to use one SMS GATEWAY.

So, here we will be using FAST2SMS GATEWAY for sending our OTP to Mobile.

For that First, we have to REGISTER with FAST2SMS.


Click on the above website link to open the fast2sms website.

 After that, we will be able to see the below interface.

open the fast2sms website

Now we can register here by filling in our proper details. After registration, we will get Rs. 54.40 and now we use it to send SMS to mobile.

But, for sending SMS or OTP to mobile using Java we have to use our unique API Key which will be provided by FAST2SMS after registration.

For getting our API Key first click on Dev API as shown below.

click on Dev API


Now we can copy our API Key.

But, the question is what's the use of this API key?. Actually, we will use this API Key with a URL for sending SMS to mobile.

Now, click on Read Documentation and we will be able to see the below interface.

Read Documentation


Now, we can see at Top-Left Corner for sending the message we can use 2 Methods.
1. GET METHOD
2. POST METHOD

Here, we will use GET METHOD for sending the SMS. And we will use the GET METHOD URL which we can see at Top-Right Corner. So, we will use our API key with this URL for sending SMS to mobile.

Here's the short OVERVIEW of the program.

short OVERVIEW of the program


For a complete tutorial watch the youtube video which I have attached at the top.


  DO SUBSCRIBE TO MY YOUTUBE CHANNEL FOR MORE TUTORIALS.  

SOURCE CODE:-

GENERATING OTP USING JAVA (SOURCE CODE):-  [CLICK HERE]

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;

public class SendOTP 
{
public static void sendOTP(String message , String number , String apiKey)
{
try
{
String sendId="FSTSMS";
String language="english";
String route="p";
message=URLEncoder.encode(message,"UTF-8");    //Important Step
String myUrl="https://www.fast2sms.com/dev/bulkauthorization="+apiKey+"&
                sender_id="+sendId+"&message="+message+"&language="+language+"&
                route="+route+"&numbers="+number;

URL url=new URL(myUrl);
HttpsURLConnection con= (HttpsURLConnection)url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent","Mozilla/5.0");
con.setRequestProperty("cache-control", "no-cache");
int responseCode=  con.getResponseCode();
StringBuffer response=new StringBuffer();
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
while(true)
{
String line=br.readLine();
if(line==null)
{
break;
}
response.append(line);
}
System.out.println(response);
}
catch(Exception e)
{
System.out.println(e);
}
}

        public static void main(String[] args) 
{
   System.out.println("Program Started....");
   
   OTP otp=new OTP();
   String otpmessage=otp.generateOTP(5);
   System.out.println( "Generate OTP : "+otpmessage);
   
   String apiKey="USE YOUR API-KEY HERE";
   String number="USE YOUR MOBILE NUMBER HERE";
   
   sendOTP("Hey this message is send by MuradAli using Java Code. Your OTP is                                     :"+otpmessage,number,apiKey);
   
}
}

Other practical projects using React & Spring Boot 

1) Employee Management System using Spring Boot Microservices & React.

2) Hotel Management System using Spring Boot & React.

3) E-commerce Project using Spring Boot & React.

4) Gym Management System using Spring Boot & React.

Students can use this project for learning and can submit the project for their final year or Internship projects.

IF YOU WANT THE PROJECT DO CONTACT ME. 

I will give you the full project SOURCE CODE and I will do a COMPLETE SET-UP of the project on your PC or Laptop remotely.

Or You can simply Request for the Project Source Code & Set Up by clicking on the below button.

MAIL ME AT codewithmurad@gmail.com

DM ME ON INSTAGRAM at @codewithmurad

DM ME ON FACEBOOK at @codewithmurad 

DM ME ON TELEGRAM at @codewithmurad 

Connect on My Discord Server: CLICK HERE

Post a Comment

0 Comments