Download the Project
You can download the complete Gradle project as a ZIP file from the link below:
How to Use the Project
Below are the step-by-step instructions, which are also included in the README.md file within the ZIP.
1. Prerequisites
- Java Development Kit (JDK) 17 or later.
- An email account to send emails from (e.g., Gmail).
2. Configuration
This is the most important step.
- Unzip the project folder.
- Open the file:
src/main/resources/application.properties. - You will see the following configuration:
# =========================================================
# IMPORTANT: CONFIGURE YOUR EMAIL CREDENTIALS
# =========================================================
# Replace with your SMTP server details
spring.mail.host=smtp.gmail.com
spring.mail.port=587
# Replace with your email and an App Password (NOT your regular password)
[email protected]
spring.mail.password=your-16-digit-app-password
# Generic SMTP properties
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
# Email addresses for the demo
# Replace this with the email address you want to send test emails TO
[email protected]
- Update
spring.mail.usernamewith your email address (e.g.,[email protected]). - Update
spring.mail.password.- For Gmail: Do NOT use your regular password. You must generate a 16-digit App Password.
- Go to your Google Account settings.
- Navigate to Security -> 2-Step Verification -> App passwords.
- Generate a new password for “Mail” on “Other (Custom name)” and use the 16-character code here.
- Update
app.mail.recipientwith the email address where you want to receive the test emails.
3. Run the Application
Open a terminal or command prompt in the root directory of the project (spring-boot-email-gradle-demo) and run the following command:
- On macOS/Linux:
./gradlew bootRun - On Windows:
gradlew.bat bootRun
The application will start on http://localhost:8080.
4. Test the Endpoints
Open a new terminal and use the following curl commands to send the different types of emails.
a) To send a Plain Text Email:
curl -X POST http://localhost:8080/send-text
b) To send an HTML Email:
curl -X POST http://localhost:8080/send-html
c) To send an Email with an Attachment:
curl -X POST http://localhost:8080/send-attachment
The attached file will be src/main/resources/static/sample-attachment.txt.
You should receive three different emails in the recipient’s inbox you configured.
Project Structure Overview
Here is a brief overview of the key files in the project:
build.gradle:- Defines the project dependencies, including
spring-boot-starter-web(for the controller) andspring-boot-starter-mail. Configured for Java 17.
- Defines the project dependencies, including
src/main/java/com/example/emaildemo/EmailDemoApplication.java:- The main entry point for the Spring Boot application.
src/main/java/com/example/emaildemo/service/EmailService.java:- This service contains all the core logic for sending emails.
sendSimpleEmail(): Sends a plain text message.sendHtmlEmail(): UsesMimeMessageandMimeMessageHelperto send HTML content.sendEmailWithAttachment(): Adds a file from the project’s resources as an attachment.
src/main/java/com/example/emaildemo/controller/EmailController.java:- A simple
@RestControllerthat exposes the three/send-*endpoints. - It calls the corresponding methods in the
EmailServiceto trigger the emails. - The recipient email address is read from the
application.propertiesfile.
- A simple
src/main/resources/application.properties:- Contains all the necessary configuration for the SMTP server and credentials.
src/main/resources/static/sample-attachment.txt:- A simple text file included in the project to be used as an attachment for the demo.