Контроллер

master
L_DelOff 2023-05-10 12:04:02 +03:00
parent efbc03f9c4
commit 7dde4ab6d1
1 changed files with 52 additions and 0 deletions

View File

@ -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);
}
}