feat(mail):ajout d'envoi de mail au format text, HTML, avec piece jointe

This commit is contained in:
Hedi 2022-01-24 23:06:27 +01:00
parent fbc5d83780
commit b47c3fdf6d
5 changed files with 168 additions and 1 deletions

View File

@ -38,7 +38,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency> <dependency>
<groupId>mysql</groupId> <groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId> <artifactId>mysql-connector-java</artifactId>

View File

@ -0,0 +1,40 @@
package fr.organizee.controller;
import fr.organizee.model.Mail;
import fr.organizee.service.SendMailService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
@RestController
@RequestMapping("/mail")
public class MailController {
SendMailService service;
public MailController(SendMailService service) {
this.service = service;
}
@PostMapping("/send")
public ResponseEntity<String> sendMail(@RequestBody Mail mail) {
service.sendMail(mail);
return new ResponseEntity<>("Email Sent successfully", HttpStatus.OK);
}
@PostMapping("/html")
public ResponseEntity<String> sendMailHTML(@RequestBody Mail mail) throws MessagingException {
service.sendMailHTML(mail);
return new ResponseEntity<>("HTML mail sent successfully", HttpStatus.OK);
}
@PostMapping("/attachment")
public ResponseEntity<String> sendAttachmentEmail(@RequestBody Mail mail) throws MessagingException {
service.sendMailWithAttachments(mail);
return new ResponseEntity<>("Attachment mail sent successfully", HttpStatus.OK);
}
}

View File

@ -0,0 +1,41 @@
package fr.organizee.model;
public class Mail {
private String recipient;
private String subject;
private String message;
public Mail() {
}
public Mail(String recipient, String subject, String message) {
this.recipient = recipient;
this.subject = subject;
this.message = message;
}
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -0,0 +1,15 @@
package fr.organizee.service;
import fr.organizee.model.Mail;
import javax.mail.MessagingException;
public interface SendMailService {
void sendMail(Mail mail);
void sendMailHTML(Mail mail) throws MessagingException;
void sendMailWithAttachments(Mail mail) throws MessagingException;
}

View File

@ -0,0 +1,68 @@
package fr.organizee.service;
import fr.organizee.model.Mail;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Service
public class SendMailServiceImpl implements SendMailService {
private final JavaMailSender javaMailSender;
public SendMailServiceImpl(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
@Override
public void sendMail(Mail mail) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo(mail.getRecipient(), mail.getRecipient());
msg.setSubject(mail.getSubject());
msg.setText(mail.getMessage());
javaMailSender.send(msg);
}
@Override
public void sendMailHTML(Mail mail) throws MessagingException {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
helper.setTo(mail.getRecipient());
helper.setSubject(mail.getSubject());
String htmlMsg = "<h3>Im testing send a HTML email</h3>"
+"<img src='http://www.apache.org/images/asf_logo_wide.gif'>";
msg.setContent(htmlMsg, "text/html");
javaMailSender.send(msg);
}
@Override
public void sendMailWithAttachments(Mail mail) throws MessagingException {
MimeMessage msg = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true);
helper.setTo("to_@email");
helper.setSubject("Testing from Spring Boot");
helper.setText("Find the attached image", true);
helper.addAttachment("hero.jpg", new ClassPathResource("hero.jpg"));
javaMailSender.send(msg);
}
}