This is a Spring Boot application for sending emails via SMTP. It supports both asynchronous and synchronous email sending, handles attachments, and integrates with RabbitMQ for message queuing.
- Send emails with optional CC and BCC addresses.
- Send emails with or without attachments.
- Support for both plain text and HTML email bodies.
- Asynchronous email sending for improved performance.
- RabbitMQ's integration for sending emails through message queues.
- Error handling and dead-letter queue support for failed messages.
- Spring Boot - A framework for building Java applications.
- Spring Mail - For sending emails.
- Spring AMQP - For message queuing and handling.
- JDK 21 or higher
- RabbitMQ server
- SMTP server for sending emails
-
Clone the repository:
git clone https://github.com/nenadjakic/mail-sender.git cd mail-sender
-
Configure the application properties: Update the
application.properties
with your SMTP server and RabbitMQ configuration.mail-sender.from=no-reply@example.com mail-sender.queue-name= mail_queue mail-sender.dead-letter-queue-name= mail_dead_letter_queue spring.mail.host=smtp.example.com spring.mail.port= 587 spring.mail.username= your-email@example.com spring.mail.password= your-email-password spring.rabbitmq.host=http://example.com spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest
-
Build the project:
./gradlew clean build
-
Run the project:
./gradlew bootRun
You can send emails via the following API endpoint:
- POST
/api/mail/send
The API accepts multipart form data. The mailDto
part is a JSON object that contains the email details,
and attachments
is an optional part for file uploads.
to
- List of recipient email addresses.cc
- List of CC email addresses (optional).bcc
- List of BCC email addresses (optional).subject
- Email subject.body
- Email body (HTML or plain text).isHtml
- Boolean indicating if the body is HTML.
curl -X POST "http://localhost:8080/api/mail/send" \
-H "Content-Type: multipart/form-data" \
-F 'mailDto={"to":["recipient@example.com"], "subject":"Test Email", "body":"This is a test email.", "isHtml":false}' \
-F "attachments=@/path/to/file.txt"
Emails can also be sent by publishing a message to RabbitMQ. The service listens for messages on the configured
queue (mail-sender.queue-name
).
- Queue:
${mail-sender.queue-name}
The message should be in JSON format with the same structure as the MailDto
object.
```json
{
"to": ["recipient@example.com"],
"cc": null,
"bcc": null,
"subject": "Test Email",
"body": "This is a test email.",
"isHtml": false
}
```
If an error occurs while sending the email, the message is routed to a **dead-letter queue
** (${mail-sender.dead-letter-queue-name}
), and an error is logged.
This project is licensed under the Apache License - see the LICENSE file for details.