A lightweight Spring Boot microservice that consumes email messages from a Kafka topic and sends them via SMTP. Ideal for building scalable, event-driven architectures with email capabilities.
- Kafka Consumer: Listens to the
signup
topic (or others). - SMTP Email Delivery: Sends emails with TLS/SSL encryption.
- Environment-driven configuration: Externalize credentials via environment variables.
- Simple & Focused: Single responsibility — email delivery.
- Java 17
- Spring Boot
- Spring for Kafka
- Apache Kafka
- JavaMail (SMTP)
- Jackson (
ObjectMapper
) - Maven
src/
├── main/
│ ├── java/com/example/emailservice/
│ │ ├── clients/
│ │ │ └── KafkaConsumerEmailClient.java # Kafka listener & email sender
│ │ ├── dtos/
│ │ │ └── EmailDto.java
│ │ └── utils/
│ │ └── EmailUtil.java
│ └── resources/
│ └── application.properties # Configurations
- Java 17+
- Kafka running (e.g.,
localhost:9092
) - SMTP server credentials (Gmail, Outlook, custom)
- Environment variables (or a
.env
file with dotenv support)
Place the real application.properties
in src/main/resources/
or use a .env
:
Kafka
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=emailService
smtp.mail=smtp.gmail.com
smtp.port=587
email.username=${EMAIL_USERNAME}
email.password=${EMAIL_PASSWORD}
./mvnw clean install
./mvnw spring-boot:run
-
Add additional @KafkaListener(topic = "otherTopic", ...) methods for more email scenarios.
-
Use @Header(KafkaHeaders.RECEIVED_TOPIC) to differentiate message types inside a listener.
-
Add unit tests using JUnit & Mockito to validate consumer logic and error handling.
-
"Could not resolve placeholder" errors → Ensure env vars or application.properties are correctly set.
-
No connection to Kafka → Verify bootstrap-servers and that Kafka is running.
-
Email authentication errors → Check SMTP settings and correct credentials.
-
❌ Never commit real credentials.
-
✅ Use environment variables or secret managers.
-
✅ Add application.properties and .env to .gitignore.
Author: Harshal Kalewar
Project: EmailService
License: MIT
GitHub: @harshalkalewar
Contributions and suggestions are welcome! Please open issues or PRs.