Refactor MailService

pull/137/head
johnniang 2019-03-27 15:12:12 +08:00
parent 58287e3ff3
commit e60460362b
2 changed files with 86 additions and 40 deletions

View File

@ -10,6 +10,11 @@ import java.util.Map;
*/
public interface MailService {
/**
* Reload email config.
*/
void reloadMailConfig();
/**
* Send a simple email
*
@ -36,7 +41,7 @@ public interface MailService {
* @param subject subject
* @param content content
* @param templateName template name
* @param attachSrc attachment path
* @param attachFilename attachment path
*/
void sendAttachMail(String to, String subject, Map<String, Object> content, String templateName, String attachSrc);
void sendAttachMail(String to, String subject, Map<String, Object> content, String templateName, String attachFilename);
}

View File

@ -1,23 +1,27 @@
package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.exception.ServiceException;
import cc.ryanc.halo.model.enums.BlogProperties;
import cc.ryanc.halo.model.enums.EmailProperties;
import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.service.OptionService;
import cc.ryanc.halo.utils.HaloUtils;
import cn.hutool.core.text.StrBuilder;
import freemarker.template.Template;
import io.github.biezhi.ome.OhMyEmail;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import java.io.File;
import java.util.Map;
import java.util.Properties;
/**
* @author : RYAN0UP
* @date : 2019-03-17
*/
@Slf4j
@Service
public class MailServiceImpl implements MailService {
@ -25,14 +29,29 @@ public class MailServiceImpl implements MailService {
private final OptionService optionService;
private boolean loaded = false;
public MailServiceImpl(FreeMarkerConfigurer freeMarker,
OptionService optionService) {
this.freeMarker = freeMarker;
this.optionService = optionService;
try {
reloadMailConfig();
} catch (Exception e) {
log.warn("You have to configure the email settings correctly before using email service");
}
}
@Override
public void reloadMailConfig() {
loaded = false;
// Get default properties
loadConfig();
}
/**
* Send a simple email
* Sends a simple email
*
* @param to recipient
* @param subject subject
@ -40,24 +59,25 @@ public class MailServiceImpl implements MailService {
*/
@Override
public void sendMail(String to, String subject, String content) {
HaloUtils.configMail(
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_HOST),
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_USERNAME),
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_PASSWORD));
loadConfig();
String fromUsername = optionService.getByPropertyOfNonNull(EmailProperties.FROM_NAME);
try {
OhMyEmail.subject(subject)
.from(optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME))
.from(fromUsername)
.to(to)
.text(content)
.send();
} catch (Exception e) {
// TODO Handle this exception.
e.printStackTrace();
log.debug("Email properties: to username: [{}], from username: [{}], subject: [{}], content: [{}]",
to, fromUsername, subject, content);
throw new ServiceException("Failed to send email to " + to, e);
}
}
/**
* Send template mail
* Sends template mail
*
* @param to recipient
* @param subject subject
@ -66,54 +86,75 @@ public class MailServiceImpl implements MailService {
*/
@Override
public void sendTemplateMail(String to, String subject, Map<String, Object> content, String templateName) {
HaloUtils.configMail(
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_HOST),
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_USERNAME),
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_PASSWORD));
StrBuilder text = new StrBuilder();
loadConfig();
String fromUsername = optionService.getByPropertyOfNonNull(EmailProperties.FROM_NAME);
try {
final Template template = freeMarker.getConfiguration().getTemplate(templateName);
StrBuilder text = new StrBuilder();
Template template = freeMarker.getConfiguration().getTemplate(templateName);
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
OhMyEmail.subject(subject)
.from(optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME))
.from(fromUsername)
.to(to)
.html(text.toString())
.send();
} catch (Exception e) {
// TODO Handle this exception.
e.printStackTrace();
log.debug("Email properties: to username: [{}], from username: [{}], subject: [{}], template name: [{}], content: [{}]",
to, fromUsername, subject, templateName, content);
throw new ServiceException("Failed to send template email to " + to, e).setErrorData(templateName);
}
}
/**
* Send mail with attachments
* Sends mail with attachments
*
* @param to recipient
* @param subject subject
* @param content content
* @param templateName template name
* @param attachSrc attachment path
* @param to recipient
* @param subject subject
* @param content content
* @param templateName template name
* @param attachFilename attachment path
*/
@Override
public void sendAttachMail(String to, String subject, Map<String, Object> content, String templateName, String attachSrc) {
HaloUtils.configMail(
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_HOST),
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_USERNAME),
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_PASSWORD));
File file = new File(attachSrc);
StrBuilder text = new StrBuilder();
public void sendAttachMail(String to, String subject, Map<String, Object> content, String templateName, String attachFilename) {
loadConfig();
String fromUsername = optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME);
File file = new File(attachFilename);
try {
final Template template = freeMarker.getConfiguration().getTemplate(templateName);
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
Template template = freeMarker.getConfiguration().getTemplate(templateName);
OhMyEmail.subject(subject)
.from(optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME))
.from(fromUsername)
.to(to)
.html(text.toString())
.html(FreeMarkerTemplateUtils.processTemplateIntoString(template, content))
.attach(file, file.getName())
.send();
} catch (Exception e) {
// TODO Handle this exception.
e.printStackTrace();
log.debug("Email properties: to username: [{}], from username: [{}], subject: [{}], template name: [{}], attachment: [{}], content: [{}]",
to, fromUsername, subject, templateName, attachFilename, content);
throw new ServiceException("Failed to send attachment email to " + to, e);
}
}
/**
* Load email config.
*/
private synchronized void loadConfig() {
if (loaded = true) {
return;
}
// Get default properties
Properties defaultProperties = OhMyEmail.defaultConfig(log.isDebugEnabled());
// Set smtp host
defaultProperties.setProperty("mail.smtp.host", optionService.getByPropertyOfNonNull(EmailProperties.SMTP_HOST));
// Config email
OhMyEmail.config(defaultProperties,
optionService.getByPropertyOfNonNull(EmailProperties.SMTP_USERNAME),
optionService.getByPropertyOfNonNull(EmailProperties.SMTP_PASSWORD));
// Set config loaded with true
loaded = true;
}
}