|
1 | 1 | package com.github.throyer.common.springboot.domain.mail.service; |
2 | 2 |
|
3 | | -import com.github.throyer.common.springboot.domain.mail.model.Email; |
| 3 | +import static com.github.throyer.common.springboot.constants.MAIL.CONTENT_IS_HTML; |
| 4 | +import static com.github.throyer.common.springboot.constants.MAIL.EMAIL_SUCCESSFULLY_SENT_TO; |
| 5 | +import static com.github.throyer.common.springboot.constants.MAIL.ERROR_SENDING_EMAIL_MESSAGE; |
| 6 | +import static com.github.throyer.common.springboot.constants.MAIL.ERROR_SMTP_AUTH; |
| 7 | +import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; |
| 8 | + |
4 | 9 | import javax.mail.MessagingException; |
| 10 | +import javax.mail.internet.MimeMessage; |
5 | 11 |
|
6 | | -import org.slf4j.Logger; |
7 | | -import org.slf4j.LoggerFactory; |
8 | 12 | import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.mail.MailAuthenticationException; |
9 | 14 | import org.springframework.mail.MailException; |
10 | 15 | import org.springframework.mail.javamail.JavaMailSender; |
11 | 16 | import org.springframework.mail.javamail.MimeMessageHelper; |
12 | 17 | import org.springframework.stereotype.Service; |
13 | 18 | import org.springframework.web.server.ResponseStatusException; |
14 | 19 | import org.thymeleaf.TemplateEngine; |
15 | 20 |
|
16 | | -import static com.github.throyer.common.springboot.constants.MAIL.*; |
17 | | -import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR; |
| 21 | +import com.github.throyer.common.springboot.domain.mail.model.Email; |
| 22 | + |
| 23 | +import lombok.extern.slf4j.Slf4j; |
18 | 24 |
|
19 | 25 | @Service |
| 26 | +@Slf4j |
20 | 27 | public class MailService { |
21 | 28 |
|
22 | | - private final TemplateEngine engine; |
23 | | - private final JavaMailSender sender; |
24 | | - |
25 | | - @Autowired |
26 | | - public MailService(TemplateEngine engine, JavaMailSender sender) { |
27 | | - this.engine = engine; |
28 | | - this.sender = sender; |
| 29 | + private final TemplateEngine engine; |
| 30 | + private final JavaMailSender sender; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + public MailService(TemplateEngine engine, JavaMailSender sender) { |
| 34 | + this.engine = engine; |
| 35 | + this.sender = sender; |
| 36 | + } |
| 37 | + |
| 38 | + public void send(Email email) { |
| 39 | + try { |
| 40 | + var message = createMessage(email); |
| 41 | + sender.send(message); |
| 42 | + log.info(EMAIL_SUCCESSFULLY_SENT_TO, email.getDestination()); |
| 43 | + } catch (MailAuthenticationException exception) { |
| 44 | + log.error(ERROR_SMTP_AUTH); |
| 45 | + } catch (MessagingException | MailException exception) { |
| 46 | + log.error(ERROR_SENDING_EMAIL_MESSAGE, exception); |
| 47 | + throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ERROR_SENDING_EMAIL_MESSAGE); |
29 | 48 | } |
| 49 | + } |
30 | 50 |
|
31 | | - private static final Logger LOGGER = LoggerFactory.getLogger(MailService.class); |
32 | | - |
33 | | - public void send(Email email) { |
34 | | - try { |
35 | | - var message = sender.createMimeMessage(); |
36 | | - var helper = new MimeMessageHelper(message); |
37 | | - helper.setTo(email.getDestination()); |
38 | | - helper.setSubject(email.getSubject()); |
39 | | - helper.setText(engine.process(email.getTemplate(), email.getContext()), CONTENT_IS_HTML); |
40 | | - sender.send(message); |
41 | | - LOGGER.info(EMAIL_SUCCESSFULLY_SENT_TO, email.getDestination()); |
42 | | - } catch (MessagingException | MailException exception) { |
43 | | - LOGGER.error(ERROR_SENDING_EMAIL_MESSAGE, exception); |
44 | | - throw new ResponseStatusException(INTERNAL_SERVER_ERROR, ERROR_SENDING_EMAIL_MESSAGE); |
45 | | - } |
46 | | - } |
| 51 | + private MimeMessage createMessage(Email email) throws MessagingException { |
| 52 | + var message = sender.createMimeMessage(); |
| 53 | + var helper = new MimeMessageHelper(message); |
| 54 | + |
| 55 | + var html = engine.process(email.getTemplate(), email.getContext()); |
| 56 | + |
| 57 | + helper.setTo(email.getDestination()); |
| 58 | + helper.setSubject(email.getSubject()); |
| 59 | + helper.setText(html, CONTENT_IS_HTML); |
| 60 | + |
| 61 | + return message; |
| 62 | + } |
47 | 63 | } |
0 commit comments