Skip to content

aboutbits/spring-boot-email-service

Repository files navigation

Spring Boot EMail Service

A reusable mailer service to send emails.

Setup

Add the mailer service to the classpath by adding the following maven dependency. Versions haven be found here

<dependency>
    <groupId>it.aboutbits.springboot</groupId>
    <artifactId>emailservice</artifactId>
    <version>x.x.x</version>
</dependency>

Attachments

If you want to use attachments, you will have to create a bean implementing this interface: AttachmentDataSource.java
This step is optional.

Usage

Sending an Email

Use the ManageEmail service to schedule an email.

Example

// @formatter:off
public void sendMail(final String to,final String subject,final String htmlBody,final String plainTextBody) {
        manageEmail.schedule(
                EmailParameter.builder()
                        .scheduleAt(OffsetDateTime.now())
                        .email(EmailParameter.Email.builder()
                                .fromName(fromName)
                                .fromAddress(fromAddress)
                                .recipient(to)
                                .subject(subject)
                                .textBody(plainTextBody)
                                .htmlBody(htmlBody)
                                .build())
                        .build()
        );
}
// @formatter:on 

Querying Emails

To read email datasets from the database use this class: QueryEmail.java

Reporting callback

If you want to receive a report after each run of the scheduler, create a Bean implementing EmailSchedulerCallback.java

Configuration

To enable this service just add @EnableEmailService to your main class. You must also enable @EnableScheduling to allow the email queue to be processed.

@SpringBootApplication
@EnableEmailService
@EnableScheduling
public class App {
    public static void main(final String[] args) {
        SpringApplication.run(App.class, args);
    }
}

The following configuration options are available:

Name Default Description
lib.emailservice.migrations.enabled true Enables database migrations.
lib.emailservice.scheduling.enabled true Enables the scheduler sending the emails.
lib.emailservice.scheduling.interval 30000 Specifies the milliseconds delay between runs of the scheduler.

Local development:

To use this library as a local development dependency, you can simply refer to the version BUILD-SNAPSHOT.

Check out this repository and run the maven goal install. This will build and install this library as version BUILD-SNAPSHOT into your local maven cache.

Note that you may have to tell your IDE to reload your main maven project each time you build the library.

Building and releasing a new version:

To create a new version of this package and push it to the maven registry, you will have to use the GitHub actions workflow and manually trigger it.

Information

About Bits is a company based in South Tyrol, Italy. You can find more information about us on our website.

Support

For support, please contact [email protected].

Credits

License

The MIT License (MIT). Please see the license file for more information.