In this repository is the common configurations that I have accross most of my Spring Boot applications.
Under the
smtp
package are the configurations for the simple SMTP configurations using gmail.
SMTPEmailCredentials
- The interface that has methods for defining thegmail
credentiaks such asusername
andpassword
used when configuring theJavaMailSender
.SMTPEmailService
- The interface defining thesendEmail
method and its variations.
SMTPEmailConfig
- A configuration class with a@Bean
method that configures theJavaMailSender
.SMTPEmailConfig2
- A configuration class with a@Bean
method that points to the implementation ofSMTPEmailService
.SMTPEmailInfo
- A helper class that groups the basic email properties such as the subject, recipient(s), body, etc.
Clone this and in
common-configs/
runmvn clean install
.Add as a dependency:
<dependency> <groupId>com.eroldmr</groupId> <artifactId>common-configs</artifactId> <version>0.0.1</version> </dependency>
Note The interface
SMTPEmailCredentials
needs to be implemented in your project and a@Bean
method that points to the implementation needs to be defined. An example is shown below:
SMTPEmailCredentialsImpl.java
package com.example.app.messaging.email;
import com.eroldmr.common.configs.smtp.SMTPEmailCredentials;
public class SMTPEmailCredentialsImpl implements SMTPEmailCredentials {
@Override
public String username() { return "[email protected]"; }
@Override
public String password() { return "password"; }
@Override
public Boolean debug() { return true; }
}
AppConfig.java
...
@Bean
SMTPEmailCredentials smtpCredentials() {
return new SMTPEmailCredentialsImpl();
}
...
OR simply
AppConfig.java
...
@Bean
SMTPEmailCredentials smtpCredentials() {
return new SMTPEmailCredentials() {
@Override
public String username() { return "[email protected]"; }
@Override
public String password() { return "password"; }
@Override
public Boolean debug() { return true; }
};
}
...
In order for our Spring Boot application to pick up our
@Service SMTPEmailService
and@Configuration
we add:@ComponentScan(basePackages = {"com.eroldmr.common.configs", "com.example.app"})to the main class of our app.
Happy hacking...