email-spring-boot-starter

Email service based on spring-boot and provide an easy way to integrate spring-mail or aws ses.

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

io.57blocks
ArtifactId

ArtifactId

email-spring-boot-starter
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

email-spring-boot-starter
Email service based on spring-boot and provide an easy way to integrate spring-mail or aws ses.
Project URL

Project URL

https://github.com/57blocks/email-spring-boot
Source Code Management

Source Code Management

https://github.com/57blocks/email-spring-boot

Download email-spring-boot-starter

How to add to project

<!-- https://jarcasting.com/artifacts/io.57blocks/email-spring-boot-starter/ -->
<dependency>
    <groupId>io.57blocks</groupId>
    <artifactId>email-spring-boot-starter</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.57blocks/email-spring-boot-starter/
implementation 'io.57blocks:email-spring-boot-starter:0.2.0'
// https://jarcasting.com/artifacts/io.57blocks/email-spring-boot-starter/
implementation ("io.57blocks:email-spring-boot-starter:0.2.0")
'io.57blocks:email-spring-boot-starter:jar:0.2.0'
<dependency org="io.57blocks" name="email-spring-boot-starter" rev="0.2.0">
  <artifact name="email-spring-boot-starter" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.57blocks', module='email-spring-boot-starter', version='0.2.0')
)
libraryDependencies += "io.57blocks" % "email-spring-boot-starter" % "0.2.0"
[io.57blocks/email-spring-boot-starter "0.2.0"]

Dependencies

compile (6)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar
org.springframework.boot : spring-boot-starter-mail jar
org.thymeleaf : thymeleaf-spring5 jar 3.0.11.RELEASE
org.thymeleaf.extras : thymeleaf-extras-java8time jar 3.0.2.RELEASE
org.projectlombok : lombok Optional jar
org.springframework.boot : spring-boot-configuration-processor Optional jar

Project Modules

There are no modules declared in this project.

Travis-CI License: MIT Maven Central

Email Spring Boot Starter

Configure an email service ready for sending emails. Supporting templating with thymeleaf.

Getting Started

Add the Starter in Maven Dependency

Edit pom.xml, add the starter:

<dependency>
  <groupId>io.57blocks</groupId>
  <artifactId>email-spring-boot-starter</artifactId>
  <version>${io.57blocks.email.version}</version>
</dependency>

Configure the JavaMailSender

By default, this starter can benefit from spring-boot-starter-mail by using SMTP protocol or JNDI to sending out emails, without other dependencies.

Default spring-boot-starter-mail Configuration

Edit application.yml, add the following properties:

spring.mail:
  host: email-smtp.us-west-2.amazonaws.com
  username: username
  password: password
  properties:
    mail.transport.protocol: smtp
    mail.smtp.port: 25
    mail.smtp.auth: true
    mail.smtp.starttls.enable: true
    mail.smtp.starttls.required: true

AWS SES Integration

If you want to use AWS SES sdk to send email via http(s) protocol, you need to import some additional dependencies:

<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-aws</artifactId>
    <version>2.0.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-java-sdk-ses</artifactId>
    <version>1.11.415</version>
  </dependency>
</dependencies>

Then update the configuration by removing spring.mail and adding AWS cloud basic configuration:

cloud:
  aws:
    region:
      static: us-east-2
    credentials:
      accessKey: accessKey
      secretKey: secretKey

Create Email Template

Default layout in /src/main/resources:

email/
  text/
    greeting.txt
  html/
    greeting.html
  subject/
    greeting.txt
  messages.properties
  messages_zh.properties

To configure template file layout, edit application.yml:

io.57blocks.email:
  enabled: true
  template:
    prefix: /email/
    message_base_name: messages
    html:
      pattern: html/*
      suffix: .html
      character_encoding: UTF-8
      cacheable: false
    text:
      pattern: text/*
      suffix: .txt
      character_encoding: UTF-8
      cacheable: false
    subject:
      pattern: subject/*
      suffix: .txt
      character_encoding: UTF-8
      cacheable: false

Send Mail

To send email, inject EmailService into the bean.

public class GreetingService {

  @Autowired
  private EmailService emailService;

  public void sendEmail() {
    try {
      Map<String, Object> ctx = ImmutableMap.of("name", "Mr. Smith");
      emailService.sendHtmlMail("Sender <[email protected]>", "greeting", Locale.CHINESE, ctx, "Mr. Smith <[email protected]>");
    } catch (MessagingException e) {
      // ignored
    }
  }
  
  public void sendEmailByMessageFormat() {
      try {
        Map<String, Object> ctx = ImmutableMap.of("name", "Mr. Smith");
        Message message = new MessageBuilder()
            .from("Sender <[email protected]>")
            .template("greeting")
            .locale(Locale.CHINESE)
            .context(ctx)
            .recipients(Arrays.asList("Mr. Smith <[email protected]>"))
            .build();
        
        emailService.sendHtmlMail(message);
      } catch (MessagingException e) {
        // ignored
      }
    }
}

Example

io.57blocks

Versions

Version
0.2.0
0.1.3
0.1.2