This project demonstrates how to send and verify OTPs (One-Time Passwords) via email using Spring Boot.
The application uses Spring Mail for email services and an in-memory store initially
- Send OTP via email: Users can receive an OTP on their email for verification.
- Verify OTP: The application validates the user-entered OTP against the stored OTP in the database.
- Expire OTPs: OTPs automatically expire after a configured time (e.g., 5 minutes).
- Java 11+
- Maven or Gradle for dependency management.
- Spring Boot framework.
- SMTP Email Account (e.g., Gmail, MailTrap, or any other provider).
git clone https://github.com/amaanalikhan3000/EmailOtp.git
cd email-otp-springbootUpdate the application.yml or application.properties file with your email service
spring:
mail:
host: smtp.gmail.com
port: 587
username: your-email@gmail.com
password: your-email-password
properties:
mail:
smtp:
auth: true
starttls:
enable: true
Build and run the Spring Boot application.
# Using Maven
mvn clean install
mvn spring-boot:run
# Or using Gradle
./gradlew build
./gradlew bootRunThe application will be available at http://localhost:8080.
Endpoint: POST /api/otp/send
Payload:
{
"email": "user@example.com"
}Response:
{
"message": "OTP sent successfully to user@example.com"
}Endpoint: POST /api/otp/verify
Payload:
{
"email": "user@example.com",
"otp": "123456"
}Response (Success):
{
"message": "OTP verification successful"
}Response (Failure):
{
"message": "Invalid or expired OTP"
}src/main/java
├── com.email.demo
│ ├── controller
│ │ ├── OtpController.java # Handles OTP-related API endpoints
│ ├── service
│ │ ├── EmailOtpService.java # Sends OTP and handles email functionality
│ │ ├── OtpStorageService.java # Manages OTP storage
│ ├── Entity
│ │ ├── OtpDetails.java # OTP Details
│
Add the following dependencies to your pom.xml:
<dependencies>
<!-- Spring Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>- OTPs are short-lived and stored in internally with an expiration time.
- Use an App Password for Gmail or secure credentials for your SMTP server.
- Email Not Sending: Ensure SMTP settings are correct and your email provider allows less secure app access or uses an App Password.
This project is licensed under the MIT License.