0% found this document useful (0 votes)
3 views

KNA-11 OTP Generator

The document details the completion of an OTP Generator project by student Praveen Biradar, which generates a random OTP and sends it via email using JavaMail API. It includes links to both a runnable JAR file and a standard JAR file, along with a description of the application structure and functionality. The project utilizes a configuration file for email credentials and provides a user interface for sending OTPs of varying lengths.

Uploaded by

shravanmb163
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

KNA-11 OTP Generator

The document details the completion of an OTP Generator project by student Praveen Biradar, which generates a random OTP and sends it via email using JavaMail API. It includes links to both a runnable JAR file and a standard JAR file, along with a description of the application structure and functionality. The project utilizes a configuration file for email credentials and provides a user interface for sending OTPs of varying lengths.

Uploaded by

shravanmb163
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

OTP Generator using Jframe and JAR files

11.Assignment Submission
Student Name: Praveen Biradar
ID: KODD8KS8I

JAR FILE LINK -

https://drive.google.com/file/d/1vRMt6flw5Ba5BZwoxYT8RGT1KnR_P4e1/view?usp=drivesdk

RUNNABLE JAR FILE -

https://drive.google.com/file/d/1JVF9O_hfwN0IyeTs2cFU7qpuWO4OUTpo/view?usp=drivesdk

Subject: Completion of OTP Generator Project

Sir, we have completed the OTP Generator project using the required details. The application generates a
random OTP and sends it via email. To implement this, I used an app password from my email
(praveenbiradar163@gmail.com) for secure OTP transmission.

We have placed the config.properties file inside the resources folder in src to ensure proper access at runtime.

Both a Runnable JAR for direct execution and a normal JAR for use in other projects have been created. The
application is ready for use. Let us know if any modifications are needed.

For email functionality, I downloaded and used the following JAR files:

• JavaMail API (javax.mail.jar) – For sending emails


• Activation JAR (activation.jar) – For handling MIME data
These JAR files were added to the project to enable email sending via SMTP.
OTPEmailSender.java file –

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Random;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class OTPEmailSender extends JFrame {


private static final long serialVersionUID = 1L;
private JTextField emailField;
private JButton send4DigitOTPButton;
private JButton send6DigitOTPButton;
private JLabel statusLabel;
private String otp;
private String senderEmail;
private String senderPassword;

public OTPEmailSender() {
loadConfig(); // Load email & password from config.properties
setTitle("OTP GENERATOR");
setSize(400, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(4, 1));
getContentPane().setBackground(new Color(173, 216, 230));

JPanel inputPanel = new JPanel();


inputPanel.setBackground(new Color(173, 216, 230));
JLabel emailLabel = new JLabel("Enter Email:");
emailField = new JTextField(20);
inputPanel.add(emailLabel);
inputPanel.add(emailField);

JPanel buttonPanel = new JPanel();


buttonPanel.setBackground(new Color(173, 216, 230));
send4DigitOTPButton = new JButton("Send 4-Digit OTP");
send6DigitOTPButton = new JButton("Send 6-Digit OTP");
buttonPanel.add(send4DigitOTPButton);
buttonPanel.add(send6DigitOTPButton);

statusLabel = new JLabel("", SwingConstants.CENTER);

add(inputPanel);
add(buttonPanel);
add(statusLabel);

send4DigitOTPButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendOTP(4);
}
});
send6DigitOTPButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
sendOTP(6);
}
});
}

private void loadConfig() {


Properties properties = new Properties();
try (InputStream input = getClass().getClassLoader().getResourceAsStream("config.properties")) {
if (input == null) {

System.out.println("⚠ config.properties file not found!");


return;
}
properties.load(input);
senderEmail = properties.getProperty("email");
senderPassword = properties.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}

private void sendOTP(int digits) {


String email = emailField.getText().trim();
if (!email.isEmpty()) {
otp = generateOTP(digits);
if (sendEmail(email, otp, digits)) {

statusLabel.setText("✅ " + digits + "-Digit OTP Sent Successfully!");


} else {

statusLabel.setText("❌ Failed to Send OTP.");


}
} else {

statusLabel.setText("⚠ Please enter an email.");


}
}

private String generateOTP(int digits) {


Random random = new Random();
int otpNumber = (digits == 6) ? (100000 + random.nextInt(900000)) : (1000 + random.nextInt(9000));
return String.valueOf(otpNumber);
}

private boolean sendEmail(String recipient, String otp, int digits) {


Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");

Session session = Session.getInstance(props, new Authenticator() {


@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(senderEmail, senderPassword);
}
});

try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(senderEmail));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
message.setSubject("Your OTP Code");

String otpMessage = otp + " is a " + digits + "-digit OTP sent to " + recipient + " by PRAVEEN BIRADAR (ID:
KODD8KS8I ). This OTP is valid for 5 minutes.";
message.setText(otpMessage);

Transport.send(message);
return true;
} catch (MessagingException e) {

statusLabel.setText("⚠ Error: " + e.getMessage());


return false;
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(() -> {
OTPEmailSender frame = new OTPEmailSender();
frame.setVisible(true);
});
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy