Sending Emails in Spring Boot 3: A Complete Guide

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.

  1. Unzip the project folder.
  2. Open the file: src/main/resources/application.properties.
  3. 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]
  1. Update spring.mail.username with your email address (e.g., [email protected]).
  2. 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.
  3. Update app.mail.recipient with 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) and spring-boot-starter-mail. Configured for Java 17.
  • 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(): Uses MimeMessage and MimeMessageHelper to 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 @RestController that exposes the three /send-* endpoints.
    • It calls the corresponding methods in the EmailService to trigger the emails.
    • The recipient email address is read from the application.properties file.
  • 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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.