mirror of https://github.com/halo-dev/halo
Refactor MailService
parent
58287e3ff3
commit
e60460362b
|
@ -10,6 +10,11 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public interface MailService {
|
public interface MailService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reload email config.
|
||||||
|
*/
|
||||||
|
void reloadMailConfig();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a simple email
|
* Send a simple email
|
||||||
*
|
*
|
||||||
|
@ -36,7 +41,7 @@ public interface MailService {
|
||||||
* @param subject subject
|
* @param subject subject
|
||||||
* @param content content
|
* @param content content
|
||||||
* @param templateName template name
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,27 @@
|
||||||
package cc.ryanc.halo.service.impl;
|
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.BlogProperties;
|
||||||
|
import cc.ryanc.halo.model.enums.EmailProperties;
|
||||||
import cc.ryanc.halo.service.MailService;
|
import cc.ryanc.halo.service.MailService;
|
||||||
import cc.ryanc.halo.service.OptionService;
|
import cc.ryanc.halo.service.OptionService;
|
||||||
import cc.ryanc.halo.utils.HaloUtils;
|
|
||||||
import cn.hutool.core.text.StrBuilder;
|
import cn.hutool.core.text.StrBuilder;
|
||||||
import freemarker.template.Template;
|
import freemarker.template.Template;
|
||||||
import io.github.biezhi.ome.OhMyEmail;
|
import io.github.biezhi.ome.OhMyEmail;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
||||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author : RYAN0UP
|
* @author : RYAN0UP
|
||||||
* @date : 2019-03-17
|
* @date : 2019-03-17
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class MailServiceImpl implements MailService {
|
public class MailServiceImpl implements MailService {
|
||||||
|
|
||||||
|
@ -25,14 +29,29 @@ public class MailServiceImpl implements MailService {
|
||||||
|
|
||||||
private final OptionService optionService;
|
private final OptionService optionService;
|
||||||
|
|
||||||
|
private boolean loaded = false;
|
||||||
|
|
||||||
public MailServiceImpl(FreeMarkerConfigurer freeMarker,
|
public MailServiceImpl(FreeMarkerConfigurer freeMarker,
|
||||||
OptionService optionService) {
|
OptionService optionService) {
|
||||||
this.freeMarker = freeMarker;
|
this.freeMarker = freeMarker;
|
||||||
this.optionService = optionService;
|
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 to recipient
|
||||||
* @param subject subject
|
* @param subject subject
|
||||||
|
@ -40,24 +59,25 @@ public class MailServiceImpl implements MailService {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void sendMail(String to, String subject, String content) {
|
public void sendMail(String to, String subject, String content) {
|
||||||
HaloUtils.configMail(
|
loadConfig();
|
||||||
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_HOST),
|
|
||||||
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_USERNAME),
|
String fromUsername = optionService.getByPropertyOfNonNull(EmailProperties.FROM_NAME);
|
||||||
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_PASSWORD));
|
|
||||||
try {
|
try {
|
||||||
OhMyEmail.subject(subject)
|
OhMyEmail.subject(subject)
|
||||||
.from(optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME))
|
.from(fromUsername)
|
||||||
.to(to)
|
.to(to)
|
||||||
.text(content)
|
.text(content)
|
||||||
.send();
|
.send();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// TODO Handle this exception.
|
log.debug("Email properties: to username: [{}], from username: [{}], subject: [{}], content: [{}]",
|
||||||
e.printStackTrace();
|
to, fromUsername, subject, content);
|
||||||
|
throw new ServiceException("Failed to send email to " + to, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send template mail
|
* Sends template mail
|
||||||
*
|
*
|
||||||
* @param to recipient
|
* @param to recipient
|
||||||
* @param subject subject
|
* @param subject subject
|
||||||
|
@ -66,54 +86,75 @@ public class MailServiceImpl implements MailService {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void sendTemplateMail(String to, String subject, Map<String, Object> content, String templateName) {
|
public void sendTemplateMail(String to, String subject, Map<String, Object> content, String templateName) {
|
||||||
HaloUtils.configMail(
|
loadConfig();
|
||||||
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_HOST),
|
|
||||||
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_USERNAME),
|
String fromUsername = optionService.getByPropertyOfNonNull(EmailProperties.FROM_NAME);
|
||||||
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_PASSWORD));
|
|
||||||
StrBuilder text = new StrBuilder();
|
|
||||||
try {
|
try {
|
||||||
final Template template = freeMarker.getConfiguration().getTemplate(templateName);
|
StrBuilder text = new StrBuilder();
|
||||||
|
Template template = freeMarker.getConfiguration().getTemplate(templateName);
|
||||||
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
|
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
|
||||||
OhMyEmail.subject(subject)
|
OhMyEmail.subject(subject)
|
||||||
.from(optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME))
|
.from(fromUsername)
|
||||||
.to(to)
|
.to(to)
|
||||||
.html(text.toString())
|
.html(text.toString())
|
||||||
.send();
|
.send();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// TODO Handle this exception.
|
log.debug("Email properties: to username: [{}], from username: [{}], subject: [{}], template name: [{}], content: [{}]",
|
||||||
e.printStackTrace();
|
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 to recipient
|
||||||
* @param subject subject
|
* @param subject subject
|
||||||
* @param content content
|
* @param content content
|
||||||
* @param templateName template name
|
* @param templateName template name
|
||||||
* @param attachSrc attachment path
|
* @param attachFilename attachment path
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void sendAttachMail(String to, String subject, Map<String, Object> content, String templateName, String attachSrc) {
|
public void sendAttachMail(String to, String subject, Map<String, Object> content, String templateName, String attachFilename) {
|
||||||
HaloUtils.configMail(
|
loadConfig();
|
||||||
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_HOST),
|
|
||||||
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_USERNAME),
|
String fromUsername = optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME);
|
||||||
optionService.getByPropertyOfNonNull(BlogProperties.MAIL_SMTP_PASSWORD));
|
|
||||||
File file = new File(attachSrc);
|
File file = new File(attachFilename);
|
||||||
StrBuilder text = new StrBuilder();
|
|
||||||
try {
|
try {
|
||||||
final Template template = freeMarker.getConfiguration().getTemplate(templateName);
|
Template template = freeMarker.getConfiguration().getTemplate(templateName);
|
||||||
text.append(FreeMarkerTemplateUtils.processTemplateIntoString(template, content));
|
|
||||||
OhMyEmail.subject(subject)
|
OhMyEmail.subject(subject)
|
||||||
.from(optionService.getByPropertyOfNonNull(BlogProperties.MAIL_FROM_NAME))
|
.from(fromUsername)
|
||||||
.to(to)
|
.to(to)
|
||||||
.html(text.toString())
|
.html(FreeMarkerTemplateUtils.processTemplateIntoString(template, content))
|
||||||
.attach(file, file.getName())
|
.attach(file, file.getName())
|
||||||
.send();
|
.send();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// TODO Handle this exception.
|
log.debug("Email properties: to username: [{}], from username: [{}], subject: [{}], template name: [{}], attachment: [{}], content: [{}]",
|
||||||
e.printStackTrace();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue