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