How to Generate OTP and Send to Mobile in Java Tutorial

Hello friends, welcome to the new tutorial. In this tutorial, we will learn How to Generate OTP and Send to Mobile in Java. For Generating OTP, we will use the Random class of Java to generate a Random number and send it to any specific mobile number, we will use the BulkSMS API.

Let’s start our tutorial and learn step by step how to Generate OTP and Send to Mobile in Java.

How to Generate OTP and Send to Mobile in Java

Register an Account with BulkSMS

  • So the first thing you need to do is Register an account with the BulkSMS to use their API.
  • Visit the website BulkSMS.com, and at the top right corner, you will see a button for Sign Up Today. Click on that to start the Registration process.

  • After that, you need to enter your Email Address and click on the Send Code Button to proceed.

  • You will get a verification code in your email. You need to copy and paste your code and then click on the Check code button.

  • If the code is correct, you will be redirected to the following page, where you have to fill in the necessary details and click on the submit button.

  • You need to remember your username and password because they will be used later in the program.
  • After clicking the Submit button, an OTP will be sent to your registered mobile number; you need to enter the OTP and click on the Submit Button, and your account will be created.

Getting the Code for Sending Message Using Java

  • In this step, what we need to do is that, we have to copy the code for sending messages using Java through BulkSMS API.
  • In order to do that, go to this webpage https://www.bulksms.com/developer/json/v1/, then scroll down a little bit, and at the right sidebar, you will find the code for Java as shown in the figure below.
  • Copy the Code.

How to Generate OTP and Send to Mobile in Java

  • After copying the code, we need to make some adjustments in that code so that we will be able to send the OTP to the desired mobile number using the Java Program.

Creating Java Project in Eclipse IDE

I will use Eclipse IDE for this program. you can use any IDE like NetBeans or IntelliJ IDEA, the coding will be the same.

  • Go the File>>New>>Java Project.
  • Give a name to your project (SendOTP in this example), and click on the Finish Button.

  • Now, right-click on the src folder under the SendOTP Project to create a new Java Class as shown in the figure below.

  • Now, give a name to your class (MainClass in this example). I have given the class name as MainClass because the code we have copied from the BulkSMS also has their class name as MainClass.
  • Next, Click on the finish button.

How to Generate OTP and Send to Mobile in Java

Final Code

  • Now, we will paste the code that we have copied from the BulkSMS, add some codes, and make some adjustments so that we will be able to send the OTP to the desired mobile number.
  • The explanations of the code are given in the comments.
  • The Final code is given below.

Adjustments Done in the Code

  • Changed the Values to Match our own Username and Password that we have created during the registration process.
  • Added code for generating random number for the purpose of OTP using Random class of Java.
  • Put the desired mobile number in which we want to send the OTP along with the message of OTP.

How to Generate OTP and Send to Mobile in Java Source Code

  • You can download the source code of the project by clicking on the button below.

Download Source Code

So this was all from this tutorial about How to Generate OTP and Send to Mobile in Java. If you have any questions regarding this post, then let me know by commenting below.

People Are Also Reading….

Leave a Comment