| | |
| | | import kr.wisestone.owl.service.UserService; |
| | | import kr.wisestone.owl.util.*; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.flywaydb.core.internal.util.scanner.filesystem.FileSystemResource; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import javax.activation.DataSource; |
| | | import javax.activation.FileDataSource; |
| | | import javax.mail.SendFailedException; |
| | | import javax.mail.internet.InternetAddress; |
| | | import javax.mail.internet.MimeMessage; |
| | | import javax.mail.internet.MimeUtility; |
| | | import javax.mail.internet.*; |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | import org.thymeleaf.context.Context; |
| | | import org.thymeleaf.spring5.SpringTemplateEngine; |
| | | |
| | |
| | | |
| | | @Value("${email.userName}") |
| | | private String emailUserName; |
| | | |
| | | @Value("${email.sender}") |
| | | private String emailSender; |
| | | |
| | | // 이메일 즉시 전송 |
| | | @Async |
| | |
| | | break; |
| | | } |
| | | |
| | | this.sendEmail(this.messageAccessor.message(mailConstants.getTitle(), locale), content, sendUsers, filePaths); |
| | | this.sendEmail(this.messageAccessor.message(mailConstants.getTitle(), locale), content, sendUsers, filePaths, null); |
| | | } |
| | | |
| | | // 이메일 템플릿 찾기 |
| | | public String makeEmailContent(EmailType emailType, Map<String, Object> params) { |
| | | MailConstants mailConstants = null; |
| | | Context context; |
| | | String content = null; |
| | | //params.put("url", this.commonConfiguration.getEmailSendUrl()); |
| | | |
| | | Locale locale = Locale.getDefault(); |
| | | |
| | | switch (emailType) { |
| | | case ISSUE_SEND_1: // 이슈 이메일 템플릿1로 대상자에게 발송 |
| | | mailConstants = MailConstants.ISSUE_SEND_1; |
| | | context = StringTemplateUtil.makeContext(params, locale); |
| | | content = this.springTemplateEngine.process(mailConstants.getMailTemplate(), context); |
| | | break; |
| | | |
| | | case ISSUE_SEND_2: // 이슈 이메일 템플릿2로 대상자에게 발송 |
| | | mailConstants = MailConstants.ISSUE_SEND_2; |
| | | context = StringTemplateUtil.makeContext(params, locale); |
| | | content = this.springTemplateEngine.process(mailConstants.getMailTemplate(), context); |
| | | break; |
| | | |
| | | case ISSUE_SEND_3: // 이슈 이메일 템플릿3로 대상자에게 발송 |
| | | mailConstants = MailConstants.ISSUE_SEND_3; |
| | | context = StringTemplateUtil.makeContext(params, locale); |
| | | content = this.springTemplateEngine.process(mailConstants.getMailTemplate(), context); |
| | | break; |
| | | } |
| | | return content; |
| | | } |
| | | |
| | | |
| | | // 이메일을 발송한다. |
| | | @Override |
| | | @Transactional(readOnly = true) |
| | | public void sendEmail(String subject, String content, String[] to, String[] filePaths) { |
| | | public void sendEmail(String subject, String content, String[] to, String[] filePaths, List<MultipartFile> multipartFiles) { |
| | | MimeMessage message = this.javaMailSender.createMimeMessage(); |
| | | |
| | | try { |
| | | InternetAddress from = new InternetAddress(this.emailUserName, "OWL-ITS"); |
| | | MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "utf-8"); |
| | | messageHelper.setSubject(subject); |
| | | messageHelper.setText(content, true); |
| | | messageHelper.setFrom(from); |
| | | messageHelper.setTo(to); |
| | | if (this.emailSender != null && !this.emailSender.equals("") |
| | | && this.emailUserName != null && !this.emailUserName.equals("")) { |
| | | |
| | | if (filePaths != null && filePaths.length > 0) { |
| | | for (String filePath : filePaths) { |
| | | if (!StringUtils.isEmpty(filePath)) { |
| | | DataSource dataSource = new FileDataSource(filePath); |
| | | messageHelper.addAttachment(MimeUtility.encodeText( |
| | | FilenameUtils.getBaseName(filePath), "utf-8", "B"), dataSource); |
| | | InternetAddress from = new InternetAddress(this.emailUserName, this.emailSender); |
| | | MimeMessageHelper messageHelper = new MimeMessageHelper(message, true, "utf-8"); |
| | | messageHelper.setSubject(subject); |
| | | messageHelper.setText(content, true); |
| | | messageHelper.setFrom(from); |
| | | messageHelper.setTo(to); |
| | | |
| | | if (multipartFiles != null && multipartFiles.size() > 0) { |
| | | for (MultipartFile file : multipartFiles) { |
| | | // 파일 첨부 |
| | | messageHelper.addAttachment(MimeUtility.encodeText(file.getOriginalFilename(), "utf-8", "B"), file); |
| | | } |
| | | } |
| | | |
| | | if (filePaths != null && filePaths.length > 0) { |
| | | for (String filePath : filePaths) { |
| | | if (!StringUtils.isEmpty(filePath)) { |
| | | DataSource dataSource = new FileDataSource(filePath); |
| | | messageHelper.addAttachment(MimeUtility.encodeText( |
| | | FilenameUtils.getBaseName(filePath), "utf-8", "B"), dataSource); |
| | | } |
| | | } |
| | | } |
| | | this.javaMailSender.send(message); |
| | | } |
| | | this.javaMailSender.send(message); |
| | | |
| | | } catch (MailSendException e) { |
| | | log.error(e.getMessage()); |
| | |
| | | // 내용이 있으면 발송 |
| | | if (!StringUtils.isEmpty(emailBuilder.toString())) { |
| | | // 이메일을 발송한다. |
| | | this.sendEmail(this.messageAccessor.message(MsgConstants.RESERVATION_EMAIL_TITLE, locale), emailBuilder.toString(), new String[]{CommonUtil.decryptAES128(sendUser)}, null); |
| | | this.sendEmail(this.messageAccessor.message(MsgConstants.RESERVATION_EMAIL_TITLE, locale), emailBuilder.toString(), new String[]{CommonUtil.decryptAES128(sendUser)}, null, null); |
| | | this.systemEmailRepository.saveAll(systemEmails); |
| | | } |
| | | } |
| | |
| | | // 내용이 있으면 발송 |
| | | if (!StringUtils.isEmpty(emailBuilder.toString())) { |
| | | // 이메일을 발송한다. |
| | | this.sendEmail(this.messageAccessor.message(MsgConstants.REALTIME_EMAIL_TITLE, locale), emailBuilder.toString(), new String[]{CommonUtil.decryptAES128(sendUser)}, null); |
| | | this.sendEmail(this.messageAccessor.message(MsgConstants.REALTIME_EMAIL_TITLE, locale), emailBuilder.toString(), new String[]{CommonUtil.decryptAES128(sendUser)}, null, null); |
| | | this.systemEmailRepository.saveAll(systemEmails); |
| | | } |
| | | } |