https://github.com/hardiksinghbehl/spring-boot-freemarker-java-mail-api
POC that sends emails to user on account creation event occurrence: made using java spring boot, apache freemarker, java-mail-API, bootstrap email editor and spring events.
https://github.com/hardiksinghbehl/spring-boot-freemarker-java-mail-api
apache-freemaker event-driven-architecture h2-database java java-mail-api junit5 mockito openapi3 spring-boot spring-events
Last synced: 10 months ago
JSON representation
POC that sends emails to user on account creation event occurrence: made using java spring boot, apache freemarker, java-mail-API, bootstrap email editor and spring events.
- Host: GitHub
- URL: https://github.com/hardiksinghbehl/spring-boot-freemarker-java-mail-api
- Owner: hardikSinghBehl
- Created: 2021-05-31T15:35:13.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-06-13T09:50:13.000Z (about 5 years ago)
- Last Synced: 2025-04-09T23:51:05.734Z (about 1 year ago)
- Topics: apache-freemaker, event-driven-architecture, h2-database, java, java-mail-api, junit5, mockito, openapi3, spring-boot, spring-events
- Language: Java
- Homepage: https://java-mail-freemarker-spring.herokuapp.com/hedwig/swagger-ui.html
- Size: 83 KB
- Stars: 4
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Hedwig
#### Proof-of-concept application to send emails to user on event occurence
[Click Here To Use Running Application](https://java-mail-freemarker-spring.herokuapp.com/hedwig/swagger-ui.html)
## Technologies Used
* Java Spring Boot
* JavaMail-API
* Spring Events
* Apache Freemarker
* JPA/H2 in-memory database
* Lombok
* Open-API (swagger-ui)
* [Bootstrap-Email Editor](https://app.bootstrapemail.com/editor)
## Spring Events
* [Reference Article](https://www.baeldung.com/spring-events)
* Event classes will represent an event for which a mail has to be sent (account-creation-event in this context can be anything depending on business logic)
* A listener class that listens to the above created event and executes code whenever the above event occures
* A publisher will publish the event (notify that event has occurred) and can be injected in the service layer
* Listener method to be executed asynchronously (recommended)
## Main classes (Drag to new window)
* [Email-Service](https://github.com/hardikSinghBehl/spring-boot-freemarker-java-mail-api/blob/main/src/main/java/com/hardik/hedwig/mail/service/EmailService.java) (to send emails to provided email-id with provided template and subject)
* [Account Creation Event](https://github.com/hardikSinghBehl/spring-boot-freemarker-java-mail-api/blob/main/src/main/java/com/hardik/hedwig/mail/event/UserAccountCreationEvent.java)
* [Account Creation Event Listener](https://github.com/hardikSinghBehl/spring-boot-freemarker-java-mail-api/blob/main/src/main/java/com/hardik/hedwig/mail/listener/UserAccountCreationListener.java) (receives userCreationRequestDto as input and sends mail to provided email-id asynchronously)
* [User Service](https://github.com/hardikSinghBehl/spring-boot-freemarker-java-mail-api/blob/main/src/main/java/com/hardik/hedwig/service/UserService.java) (class that creates user account in the system and publishes that AccountCreationEvent has occurred)
## Bootstrap Email
* HTML for emails is slightly different from HTML for web
* Copy the HTML generated from bootstrap email editor (right part of screen) to a .ftl file under **src/main/resources/templates** (configured in FreemarkerConfiguration.class)
* **account-creation-success.ftl** in templates folder will be generated to the below image
* ${emailId} and ${fullName} will be converted to provided emailId and fullname with the help of apache freemarker

## Local Setup
* Install Java 15
* Install Maven
Recommended way is to use [sdkman](https://sdkman.io/) for installing both maven and java
Go to application.properties and configure your email and [app password](https://devanswers.co/create-application-specific-password-gmail/) (diffrent from regular passwords)
```
spring.mail.username =
spring.mail.password =
```
Run the below commands in the core
```
mvn clean install
```
```
mvn spring-boot:run
```
server port is configured to 9090 and base-url to /hedwig which can be changed in application.properties file
Go to the below url to view swagger-ui (API docs)
```
http://localhost:9090/hedwig/swagger-ui.html
```
Run Tests
```
mvn test
```