As you all guyz know that security of user account is the critical problem in these days. So, the developer have to think about how they can secure their user account. This is can done by generating and sending OPT to the user phone number through SMS.

First understand what is OTP?

A One Time Password (OTP) is a unique code/token generated by our application, sent to a user via SMS text and then entered into our application login/signup flow for additional security.

  • People supply a mobile number when they join your service.
  • You generate a unique code and merge it into an SMS text and send to the user.
  • The user types the code into a form on your server and you verify the code matches.
  • You delete the code after a successful login or after a period of time.

One-Time Passwords are only effective for a fixed period of time and become invalid once the user logs in, making them exceptionally useful against spyware such as key logging programs.

One you have built One Time Passwords into your activation/signup flow a One Time Password will be texted every time a login attempt is made.  This flow will dramatically improve account security.

“This tutorial may be very useful for those who want to integrate OTP features in their project”.

So for this, we have to take the help of SMS gateway to send the OTP to the user Phone Number.

We are going to take help of txtlocal as our SMS gateway. All you have do is go to http://www.txtlocal.co.uk and there you need to create one account. after creating account. (you need to fill up some of your information and txtlocal will send you a verification email into your email and verify your account [watch the video for detail]). After completing this step, go http://api.txtlocal.com/docs/sendsms and from the select language option choose java and then copy the code and edit as in the following manner or simply copy the following code but you need to provide your own api key when you run this project.

Now write the following piece of code after designing the following form in NetBeans IDE.

STEPS:

1.Now open Netbeans IDE and click on file > new project. Select categories as java and project as java application from that window:

How to Create Login From in Java

2. Click next and then give your project name and then click finished.

3. Now right click on your project and select new > JFrame Form:

4. After this new window will appear from this window drag Panel and extend it, and drag three label, three Text Field, two Button and drop to the jframe from. Change the name of the label to Name, Phoneand Verify OTP. Change the variable name of the Text Field by right clicking it and named it as txtName and txtPhone and txtVerOTP respectively. Change the Button text to Login and Verify.

Three Label for phone no, name, verify OTP
Three Text Field for phone no, name , OTP
Two Button to send OTP and Verify OTP

How to Generate and Send OTP SMS for Free Using java?
How to Generate and Send OTP SMS for Free Using java?

Double click on the Login button to go to the source of your project and scroll up and then import following class inside your package name:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Random;
import javax.swing.JOptionPane;

Define one global variable inside the class:

int OTP

Write the following piece of code inside the clicking event of Login Button:


try {
// Construct data
String apiKey = "apikey=" + "your_api_key";
Random rand = new Random();
OTP=rand.nextInt(999999);
String name = txtName.getText();

String message = "&message=" + "Hey "+name+ " your OTP IS "+OTP;
String sender = "&sender=" + "your_sender_name";
String numbers = "&numbers=" +txtPhone.getText();

// Send data
HttpURLConnection conn = (HttpURLConnection) new URL("https://api.txtlocal.com/send/?").openConnection();
String data = apiKey + numbers + message + sender;
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length", Integer.toString(data.length()));
conn.getOutputStream().write(data.getBytes("UTF-8"));
final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
final StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
stringBuffer.append(line);
}
rd.close();
JOptionPane.showConfirmDialog(null, "OTP send Successfully");

//return stringBuffer.toString();
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Error SMS "+e);
//return "Error "+e;
JOptionPane.showMessageDialog(null, "error "+e);


}
}

Double click the verify button and write following piece of code:

if(Integer.parseInt(txtVerOTP.getText())==OTP){
JOptionPane.showMessageDialog(null, "you are login successfully");
this.setVisible(false); //hide current jframe form
new home().setVisible(true); //open new jframe form having name home

//NOTE: TO ADD NEW FORM (home)  REPEAT STEP 3
}else{
JOptionPane.showMessageDialog(null, "wrong OTP");
}

Now Login to the your txtlocal account, goto setting>API KEY and then click on create API Key and then simply copy that created key.

Run your project by right clicking your jframe form and click Run File. Enter your Name, Phone number with country code in the respective textbox and then click on Login button. And when you receive OTP enter that OTP in OTP textbox. That’s it.

May be after sending 10 OTP SMS your credit will be finished! but don’t be sad you can send more and more OTP SMS but you need to create another account with another email account in www.txtlocal.co.uk ! that’s it. you may not be that much happy to hear this (sending only 10 OTP SMS) but don’t forget you are able to send these 10 OTP SMS for free and also there is one line “To get something, you must lose something”. it means you have to spend some time in creating account in txtlocal.co.uk in order to send free OTP SMS.

Finally, if you guyz think, this is awesome tutorial and help you in some extend please share this tutorials with your all friend so that they can know about this method of sending OTP SMS!

if you have any confusion, you can watch the following video:

 

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here