From 7dde4ab6d165d7a41c030b4a4e201833c8423d16 Mon Sep 17 00:00:00 2001 From: L_DelOff <51275636+LDelOff@users.noreply.github.com> Date: Wed, 10 May 2023 12:04:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB?= =?UTF-8?q?=D0=BB=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ldeloff/controller/EmailController.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/main/java/ru/ldeloff/controller/EmailController.java diff --git a/src/main/java/ru/ldeloff/controller/EmailController.java b/src/main/java/ru/ldeloff/controller/EmailController.java new file mode 100644 index 0000000..3554766 --- /dev/null +++ b/src/main/java/ru/ldeloff/controller/EmailController.java @@ -0,0 +1,52 @@ +package ru.ldeloff.controller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.mail.MailException; +import org.springframework.web.bind.annotation.*; +import ru.ldeloff.service.EmailService; + +import javax.mail.MessagingException; +import java.io.FileNotFoundException; + + +@RestController +@RequestMapping("/email") +public class EmailController { + + private static final Logger LOG = LoggerFactory.getLogger(EmailController.class); + + @Autowired + EmailService emailService; + + @GetMapping(value = "/simple-email/{user-email}") + public @ResponseBody ResponseEntity sendSimpleEmail(@PathVariable("user-email") String email) { + + try { + emailService.sendSimpleEmail(email, "Welcome", "This is a welcome email for your!!"); + } catch (MailException mailException) { + LOG.error("Error while sending out email..{}", mailException.getStackTrace()); + return new ResponseEntity<>("Unable to send email", HttpStatus.INTERNAL_SERVER_ERROR); + } + + return new ResponseEntity<>("Please check your inbox", HttpStatus.OK); + } + + @GetMapping(value = "/simple-order-email/{user-email}") + public @ResponseBody ResponseEntity sendEmailAttachment(@PathVariable("user-email") String email) { + + try { + emailService.sendEmailWithAttachment(email, "Order Confirmation", "Thanks for your recent order", + "classpath:purchase_order.pdf"); + } catch (MessagingException | FileNotFoundException mailException) { + LOG.error("Error while sending out email..{}", mailException.getStackTrace()); + return new ResponseEntity<>("Unable to send email", HttpStatus.INTERNAL_SERVER_ERROR); + } + + return new ResponseEntity<>("Please check your inbox for order confirmation", HttpStatus.OK); + } + +} \ No newline at end of file