|
package id.worx.worx.service; |
|
|
|
import id.worx.worx.config.properties.WorxProperties; |
|
import lombok.extern.slf4j.Slf4j; |
|
import org.springframework.context.MessageSource; |
|
import org.springframework.mail.javamail.JavaMailSender; |
|
import org.springframework.mail.javamail.MimeMessageHelper; |
|
import org.springframework.scheduling.annotation.Async; |
|
import org.springframework.stereotype.Service; |
|
import org.thymeleaf.spring5.SpringTemplateEngine; |
|
|
|
import javax.mail.internet.MimeMessage; |
|
import java.nio.charset.StandardCharsets; |
|
|
|
@Service |
|
@Slf4j |
|
public class MailService { |
|
|
|
private static final String USER = "user"; |
|
|
|
private static final String BASE_URL = "baseUrl"; |
|
|
|
private final WorxProperties worxProperties; |
|
private final JavaMailSender javaMailSender; |
|
|
|
private final MessageSource messageSource; |
|
|
|
private final SpringTemplateEngine templateEngine; |
|
|
|
public MailService(WorxProperties worxProperties, JavaMailSender javaMailSender, |
|
MessageSource messageSource, SpringTemplateEngine templateEngine) { |
|
this.worxProperties = worxProperties; |
|
|
|
this.javaMailSender = javaMailSender; |
|
this.messageSource = messageSource; |
|
this.templateEngine = templateEngine; |
|
} |
|
|
|
@Async |
|
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) { |
|
log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", |
|
isMultipart, isHtml, to, subject, content); |
|
|
|
// Prepare message using a Spring helper |
|
MimeMessage mimeMessage = javaMailSender.createMimeMessage(); |
|
try { |
|
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, StandardCharsets.UTF_8.name()); |
|
message.setTo(to); |
|
message.setFrom(worxProperties.getMail().getFromAddress()); |
|
message.setSubject(subject); |
|
message.setText(content, isHtml); |
|
javaMailSender.send(mimeMessage); |
|
log.debug("Sent email to User '{}'", to); |
|
} catch (Exception e) { |
|
if (log.isDebugEnabled()) { |
|
log.warn("Email could not be sent to user '{}'", to, e); |
|
} else { |
|
log.warn("Email could not be sent to user '{}': {}", to, e.getMessage()); |
|
} |
|
} |
|
} |
|
|
|
@Async |
|
public void sendEmailTemplate(String to, String subject, String content, boolean isMultipart, boolean isHtml) { |
|
log.debug("Send email[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}", |
|
isMultipart, isHtml, to, subject, content); |
|
|
|
// Prepare message using a Spring helper |
|
MimeMessage mimeMessage = javaMailSender.createMimeMessage(); |
|
try { |
|
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, StandardCharsets.UTF_8.name()); |
|
message.setTo(to); |
|
message.setFrom(worxProperties.getMail().getFromAddress()); |
|
message.setSubject(subject); |
|
message.setText(content, isHtml); |
|
javaMailSender.send(mimeMessage); |
|
log.debug("Sent email to User '{}'", to); |
|
} catch (Exception e) { |
|
if (log.isDebugEnabled()) { |
|
log.warn("Email could not be sent to user '{}'", to, e); |
|
} else { |
|
log.warn("Email could not be sent to user '{}': {}", to, e.getMessage()); |
|
} |
|
} |
|
} |
|
|
|
} |