Skip to content

skn437/skn-spring-mail

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SKN Reactive Spring Boot Mail Library

java

Java

Maven Central Javadoc Apache License 2.0

Β 

JavaDocs:

Read the Javadoc for the main Service APIs

Β 

Introduction:

This is a simple Java Reactive Spring Boot Library for sending mails

I made this library so that I can use it in most of my spring boot reactive projects without writing the same codes over and over again

The main API Classes of this library are MailSenderService which has 4 methods to send mails, MailSenderRequestInfo which holds the blueprint for @RequestBody/@RequestPart annotated params, MailSenderInputStream which holds the blueprint for processing proper input stream info & MailSenderHtmlTemplate which holds the blueprint for processing proper thymeleaf HTML template info. These APIs are for controllers

Β 

Details:

(1) MailSenderService Class:

  • It needs to be instantiated first
  • It must be used in controller POST requests
  • It has 4 methods to send mails
  • These 4 methods throw MessagingException if sending error occurs
  • 2 methods out of 4 also throw IOException if file attachment error occurs
  • The modes to send mails:
    • Basic Mail (It throws MessagingException)
    • Basic Mail With Attachment (It throws MessagingException & IOException)
    • Mail With HTML Template (It throws MessagingException)
    • Mail With HTML Template & Attachment (It throws MessagingException & IOException)

(2) MailSenderRequestInfo Class:

  • It is the blueprint for @RequestBody/@RequestPart annotated params in controllers
  • In controller POST requests, the request body or request part must match the blueprint of it

(3) MailSenderInputStream Class:

  • It is the blueprint for processing proper input stream info in controllers
  • In controller POST requests, it will receive MultipartFile type as @RequestPart
  • Then this API instance should be created with the input stream

(4) MailSenderHtmlTemplate Class:

  • It is the blueprint for processing proper thymeleaf HTML template info in controllers
  • The project should have a Thymeleaf Html Template already
  • Then this API instance should be created with the template name and the variable name inside that template

Β 

Requirements:

  • πŸ’€ Minimum Java Version: 21
  • πŸ’€ Minimum Maven Version: 3.9.6
  • πŸ’€ Minimum Spring Boot Version: 3.2.5
  • πŸ’€ Spring Web Flux (Reactive Spring Boot)
  • πŸ’€ Spring Java Mail Sender
  • πŸ’€ Spring Thymeleaf

Β 

Usage:

For Maven, inside dependencies tag of pom.xml, copy the following

<dependency>
  <groupId>best.skn</groupId>
  <artifactId>skn-spring-mail</artifactId>
  <version>2.0.2</version>
</dependency>

Write the following in the terminal

mvn install

First create a configuration class

@Configuration
@Import(best.skn.mail.configurations.MailSenderConfiguration.class)
public class MailSenderConfiguration {}

Inside your Java Code, import the package like this for MailSenderService

import best.skn.mail.services.MailSenderService;

Inside your Java Code, import the package like this for MailSenderRequestInfo, MailSenderInputStream & MailSenderHtmlTemplate

import best.skn.mail.models.*;

In controller POST requests, use it like the following (Just an example)

@Autowired
private MailSenderService mailSender;

@PostMapping("/endpoint-for-basic-mail")
public Mono<String> sendMail(@RequestBody MailSenderRequestInfo info)
throws MessagingException {
  return this.mailSender.sendMail(info);
}

@PostMapping("/endpoint-for-mail-with-attachment")
public Mono<String> sendMailWithAttachment(
  @RequestPart MailSenderRequestInfo info,
  @RequestPart MultipartFile file
) throws MessagingException, IOException {
  MailSenderInputStream stream = new MailSenderInputStream(
    "output file location here",
    file.getInputStream()
  );

  return this.mailSender.sendMailWithAttachment(info, stream);
}

@PostMapping("/endpoint-for-mail-with-html-template")
public Mono<String> sendMailWithHtmlTemplate(
  @RequestBody MailSenderRequestInfo info
) throws MessagingException {
  // you must have "mail.html" in `resources/templates` and the template must have a `message` variable
  MailSenderHtmlTemplate template = new MailSenderHtmlTemplate(
    "mail.html",
    "message"
  );

  return this.mailSender.sendMailWithHtmlTemplate(info, template);
}

@PostMapping("/endpoint-for-mail-with-html-template-and-attachment")
public Mono<String> sendMailWithHtmlTemplateAndAttachment(
  @RequestPart MailSenderRequestInfo info,
  @RequestPart MultipartFile file
) throws MessagingException, IOException {
  // you must have "mail.html" in `resources/templates` and the template must have a `message` variable
  MailSenderHtmlTemplate template = new MailSenderHtmlTemplate(
    "mail.html",
    "message"
  );

  MailSenderInputStream stream = new MailSenderInputStream(
    "output file location here",
    file.getInputStream()
  );

  return this.mailSender.sendMailWithHtmlTemplateAndAttachment(info, template, stream);
}

When requesting the API from Postman or Frontend Framework for mails without attachment, the request body json format can be like the following-

{
	"from": "<sender email address>",
	"to": "<receiver email address>",
	"subject": "<mail subject>",
	"body": "<mail body>"
}

When requesting the API from Postman or Frontend Framework for mails with attachment, the request should be made with form-data format as it will be processed with @RequestPart

For instructions for Gradle & others: Visit Maven Central

Β 

Dedicated To:

  • πŸ‘©β€πŸŽ¨Prodipta Das Logno & πŸ§›β€β™€οΈAtoshi Sarker Prithula: The two most special ladies of my life. I can't thank them enough for always treasuring me a lot. I am lucky that I met with these two amazing ladies. They have two special places in my heart and no other girl can ever replace them.
  • πŸ’―My Father & Mother: The greatest treasures of my life ever.