Mail System
Introduction
Ontimize Boot is a framework that allows you to simplify the configuration of a project made with Ontimize EE, in a fast and efficient way. The email system allows you to send mail from the server with a simple configuration.
Prerequisites
There are 2 options to follow this tutorial, clone the repository with the initial state and follow the tutorial step by step, or download the final example and see which files are new and which have been updated.
Initial project
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-mail-initial
Final example
/$ git clone https://github.com/ontimize/ontimize-examples
/ontimize-examples$ cd ontimize-examples
/ontimize-examples$ git checkout boot-mail
Configuring email service with Ontimize Boot
In this complete tutorial, we are going to create a backend for an application from scratch, including the database with different tables, configuring the server, creating the necessary DAO files and implementing the service. At the end of the tutorial, we will even be testing that our mailing system is fully operational and functional.
DB configuration
If the configuration table does not exist in the DB, it can be created with the following command:
Sentencia SQL (HSQL)
We fill in this table with the data that applies to each specific mail server, in this example, we will use the following:
SETTING_KEY | SETTING_VALUE | SETTING_COMMENT |
---|---|---|
mail_host | localhost | Server host |
mail_port | 2525 | Email server port |
mail_protocol | smtp | Mailing protocol |
mail_user | my.mail@example.com | User for sending |
mail_password | my_password | Mail server password |
mail_encoding | UTF-8 | Encoding of mails |
mail_properties | mail.smtp.auth:true; mail.smtp.starttls.enable:true; | Mail properties |
Sentencia SQL (HSQL)
The implementation interface contains multiple methods, to which we must give permissions, if our application has permissions:
Sentencia SQL (HSQL)
Adding dependencies
Now we need to add the correct dependency in the correct pom.xml:
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Server Configuration
To configure this service, a new configuration fragment shall be added to the application.yml file.
This configuration indicates the keys and values to be stored in the database. The database table is the one corresponding to the bean described in the refRepository: OCSettingsDao attribute (in this case, OCSettingsDao), which can be seen in the table TSETTING
attribute (for this example, TSETTING
) of the *.xml configuration file of the bean (OCSettingsDao.xml). The keys would be stored in the SETTING_KEY
column, the values in the SETTING_VALUE
column and the rest of the attributes map the keys that exist in the database.
In addition, the packet that will be scanned to look for the implementation of the email service is indicated.
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Creation of DAO files
Let’s create a DAO (Data Access Object) in the projectwiki-model
module to use as a model of this database table. The DAO is composed by 2 files, a file with extension *.xml and a *.java file.
In our *.xml file we will indicate the database table for which DAO belongs, the data source from which we collect the information (e.g. the database connection that contains this table) and the schema to which the table belongs.
OCSettingsDao.xml
In the *.java file we indicate that it is a repository whose name will be OCSettingsDao, through the annotation @Repository
. With the annotation @Lazy
, we will indicate that the load is delayed until it is completely necessary (improving in that way the performance), and the annotation @ConfigurationFile
allows us to configure this DAO using the XML file and an additional file where some common characteristics to several DAOs can be stored. like the scheme to which they belong.
OCSettingsDao.java
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- OCSettingsDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- OCSettingsDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Implementation in an existing service
To use this service in another service (e.g. to send a mail when a new record is created), just add a variable of type com.ontimize.jee.common.services.mail.IMailService and annotate it with @Autowired
.
For space reasons, the entire files are not included, only the code snippets related to the tutorial. The … in the code snippets indicate that there may be unrelated code before or after them.
- ontimize-examples
- projectwiki-api
- src
- main
- java
- com
- ontimize
- projectwiki
- api
- core
- service
- ICandidateService.java
- IUserService.java
- service
- core
- api
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- projectwiki-boot
- src
- main
- java
- com
- ontimize
- projectwiki
- ServerApplication.java
- projectwiki
- ontimize
- com
- resources
- application.yml
- java
- main
- pom.xml
- src
- projectwiki-model
- src
- main
- db
- templateDB.properties
- templateDB.txt
- java
- com
- ontimize
- projectwiki
- model
- core
- dao
- CandidateDao.java
- OCSettingsDao.java
- UserDao.java
- UserRoleDao.java
- service
- CandidateService.java
- UserService.java
- dao
- core
- model
- projectwiki
- ontimize
- com
- resources
- dao
- CandidateDao.xml
- OCSettingsDao.xml
- placeholders.properties
- RoleDao.xml
- RoleServerPermissionDao.xml
- ServerPermissionDao.xml
- UserDao.xml
- UserRoleDao.xml
- dao
- db
- main
- pom.xml
- src
- projectwiki-ws
- src
- main
- java
- com
- ontimize
- projectwiki
- ws
- core
- rest
- CandidateRestController.java
- MainRestController.java
- TestRestController.java
- UserRestController.java
- rest
- core
- ws
- projectwiki
- ontimize
- com
- java
- main
- pom.xml
- src
- .gitignore
- pom.xml
- README.md
- projectwiki-api
Checking the sending of mails
In order to check that the created service is indeed sending mails, we will use an external service called FakeSMTP, is a free Fake SMTP Server with GUI for testing emails in applications easily.
To do this, we enter this link and clone the repository it indicates into the workspace where we have our project.
We launch the DB and the server. Next, open a console and move to the path where FakeSMTP was downloaded:
Command | Meaning | 34 |
---|---|---|
fakeSMTP-VERSION.jar | Downloaded version. | |
-s | Launch the server. | |
- p 2525 | Launch the application on the port indicated. |
Now we can use an application like Postman to execute different REST requests to our project.
Use REST request
The requests contains the following structure: localhost:33333/candidates/candidate
Element | Meaning |
---|---|
localhost:33333 | Indicates the host |
/candidates | Indicates the service to be queried |
/candidate | Indicates the DAO that will access that service |
The request types can only be GET, POST, PUT, DELETE.
Below are examples of request for candidates (CANDIDATES).
The authorization used for these requests is authorization of the type BASIC.
In both cases, the access must be done with a user and password example:
User: demo
Password: demouser
Request type | Query | URL | Service method | Body request |
---|---|---|---|---|
POST | insert | localhost:33333/candidates/candidate | candidateInsert | Example below |
Body request: