Java
Β
- MailSenderService API
- MailSenderRequestInfo API
- MailSenderInputStream API
- MailSenderHtmlTemplate API
Β
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
Β
- 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
)
- Basic Mail (It throws
- 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
- 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
- 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
Β
- π Minimum Java Version:
21
- π Minimum Spring Boot Version:
3.3.5
- π Spring Web Flux (Reactive Spring Boot)
- π Spring Java Mail Sender
- π Spring Thymeleaf
Β
<dependency> <groupId>best.skn</groupId> <artifactId>skn-spring-mail</artifactId> <version>2.4.0</version> </dependency>
implementation("best.skn:skn-spring-mail:2.4.0")
import best.skn.mail.configurations.MailSenderConfiguration; @Configuration @Import(MailSenderConfiguration.class) public class MailSenderSpringConfiguration {}
import best.skn.mail.services.MailSenderService;
Inside your Java Code, import the package like this for MailSenderRequestInfo
, MailSenderInputStream
& MailSenderHtmlTemplate
import best.skn.mail.models.*;
@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
Β
- π©βπ¨
Prodipta Das Logno
& π§ββοΈAtoshi Sarker Prithula
: The two most special ladies of my life. My best wishes will always be with you two. May you two always be happy. - π―
My Parents
: The greatest treasures of my life ever.
Β
Copyright (C) 2024 SKN Shukhan
Licensed under the Apache License, Version 2.0