feat: 添加默认邮件通知器
This commit is contained in:
parent
216a059169
commit
19de2dd4f9
@ -16,4 +16,73 @@
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.cicdi</groupId>
|
||||
<artifactId>notify-core</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context-support</artifactId>
|
||||
<version>5.2.15.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.73</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>jakarta.mail</artifactId>
|
||||
<version>1.6.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-web</artifactId>
|
||||
<version>5.2.10.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hswebframework.web</groupId>
|
||||
<artifactId>hsweb-core</artifactId>
|
||||
<version>4.0.11-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot</artifactId>
|
||||
<version>2.3.11.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<version>2.3.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
<version>2.3.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
<version>2.3.11.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jsoup</groupId>
|
||||
<artifactId>jsoup</artifactId>
|
||||
<version>1.11.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
@ -0,0 +1,31 @@
|
||||
package com.simaek.notify.email;
|
||||
|
||||
import com.cicdi.notify.Provider;
|
||||
|
||||
/**
|
||||
* 邮件通知提供商
|
||||
*
|
||||
* @author xueye
|
||||
*/
|
||||
public enum EmailProvider implements Provider {
|
||||
|
||||
embedded("默认"),
|
||||
;
|
||||
|
||||
private String name;
|
||||
|
||||
EmailProvider(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return name();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package com.simaek.notify.email;
|
||||
|
||||
import com.cicdi.notify.template.Template;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 邮件模板
|
||||
*
|
||||
* @author xueye
|
||||
*/
|
||||
public class EmailTemplate implements Template {
|
||||
|
||||
/**
|
||||
* 收件人
|
||||
*/
|
||||
private List<String> sendTo;
|
||||
|
||||
/**
|
||||
* 邮件主题
|
||||
*/
|
||||
private String subject;
|
||||
|
||||
/**
|
||||
* 邮件文本
|
||||
*/
|
||||
private String text;
|
||||
|
||||
/**
|
||||
* 邮件附件
|
||||
*/
|
||||
private List<Attachment> attachments;
|
||||
|
||||
public static class Attachment {
|
||||
|
||||
/**
|
||||
* 附件名称
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 附件位置
|
||||
*/
|
||||
private String location;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public List<String> getSendTo() {
|
||||
return sendTo;
|
||||
}
|
||||
|
||||
public void setSendTo(List<String> sendTo) {
|
||||
this.sendTo = sendTo;
|
||||
}
|
||||
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public List<Attachment> getAttachments() {
|
||||
return attachments;
|
||||
}
|
||||
|
||||
public void setAttachments(List<Attachment> attachments) {
|
||||
this.attachments = attachments;
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
package com.simaek.notify.email;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 转换过的邮件模板
|
||||
*
|
||||
* @author xueye
|
||||
*/
|
||||
public class EmailTemplateParsed {
|
||||
|
||||
/**
|
||||
* 邮件主题
|
||||
*/
|
||||
private String subject;
|
||||
|
||||
/**
|
||||
* 邮件正文
|
||||
*/
|
||||
private String text;
|
||||
|
||||
/**
|
||||
* 图片 key:text中图片占位符 value:图片uri
|
||||
* 邮件图片
|
||||
*/
|
||||
private Map<String, String> images;
|
||||
|
||||
/**
|
||||
* 附件 key:附件名称 value:附件uri
|
||||
* 邮件附件
|
||||
*/
|
||||
private Map<String, String> attachments;
|
||||
|
||||
public EmailTemplateParsed() {
|
||||
}
|
||||
|
||||
public EmailTemplateParsed(EmailTemplateParsed target) {
|
||||
this.subject = target.subject;
|
||||
this.text = target.text;
|
||||
this.images = target.images;
|
||||
this.attachments = target.attachments;
|
||||
}
|
||||
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public Map<String, String> getImages() {
|
||||
return images;
|
||||
}
|
||||
|
||||
public void setImages(Map<String, String> images) {
|
||||
this.images = images;
|
||||
}
|
||||
|
||||
public Map<String, String> getAttachments() {
|
||||
return attachments;
|
||||
}
|
||||
|
||||
public void setAttachments(Map<String, String> attachments) {
|
||||
this.attachments = attachments;
|
||||
}
|
||||
|
||||
public static Builder builder(){
|
||||
return new Builder();
|
||||
};
|
||||
|
||||
public static class Builder {
|
||||
private final EmailTemplateParsed target;
|
||||
|
||||
public Builder() {
|
||||
this.target = new EmailTemplateParsed();
|
||||
}
|
||||
|
||||
public Builder subject(String subject) {
|
||||
target.subject = subject;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder text(String text) {
|
||||
target.text = text;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder images(Map<String, String> images) {
|
||||
target.images = images;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder attachments(Map<String, String> attachments) {
|
||||
target.attachments = attachments;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EmailTemplateParsed build() {
|
||||
return new EmailTemplateParsed(target);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,208 @@
|
||||
package com.simaek.notify.email.embedded;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.cicdi.notify.*;
|
||||
import com.cicdi.notify.template.TemplateManager;
|
||||
import com.simaek.notify.email.EmailProvider;
|
||||
import com.simaek.notify.email.EmailTemplate;
|
||||
import com.simaek.notify.email.EmailTemplateParsed;
|
||||
import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility;
|
||||
import org.hswebframework.web.exception.BusinessException;
|
||||
import org.hswebframework.web.id.IDGenerator;
|
||||
import org.hswebframework.web.utils.ExpressionUtils;
|
||||
import org.hswebframework.web.validator.ValidatorUtils;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Document;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.InputStreamSource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mail.javamail.JavaMailSender;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.mail.javamail.MimeMessageHelper;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 使用javax.mail进行邮件发送
|
||||
*
|
||||
* @author bsetfeng
|
||||
* @author zhouhao
|
||||
* @since 1.0
|
||||
**/
|
||||
public class DefaultEmailNotifier extends AbstractNotifier<EmailTemplate> {
|
||||
|
||||
private JavaMailSender javaMailSender;
|
||||
|
||||
public JavaMailSender getJavaMailSender() {
|
||||
return javaMailSender;
|
||||
}
|
||||
|
||||
public void setJavaMailSender(JavaMailSender javaMailSender) {
|
||||
this.javaMailSender = javaMailSender;
|
||||
}
|
||||
|
||||
private String sender;
|
||||
|
||||
public String getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(String sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
private final String notifierId;
|
||||
|
||||
@Override
|
||||
public String getNotifierId() {
|
||||
return notifierId;
|
||||
}
|
||||
|
||||
// public static Scheduler scheduler = Schedulers.newElastic("email-notifier");
|
||||
|
||||
public DefaultEmailNotifier(NotifierProperties properties, TemplateManager templateManager) {
|
||||
super(templateManager);
|
||||
notifierId = properties.getId();
|
||||
|
||||
DefaultEmailProperties emailProperties = new JSONObject(properties.getConfiguration())
|
||||
.toJavaObject(DefaultEmailProperties.class);
|
||||
// ValidatorUtils.tryValidate(emailProperties);
|
||||
|
||||
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
|
||||
mailSender.setHost(emailProperties.getHost());
|
||||
mailSender.setPort(emailProperties.getPort());
|
||||
mailSender.setUsername(emailProperties.getUsername());
|
||||
mailSender.setPassword(emailProperties.getPassword());
|
||||
mailSender.setJavaMailProperties(emailProperties.createJavaMailProperties());
|
||||
this.sender = emailProperties.getSender();
|
||||
this.javaMailSender = mailSender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(EmailTemplate template, Map<String, Object> context) {
|
||||
EmailTemplateParsed emailTemplateParsed = convert(template, context);
|
||||
try {
|
||||
this.doSend(emailTemplateParsed, template.getSendTo());
|
||||
} catch (MessagingException | UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public NotifyType getType() {
|
||||
return DefaultNotifyType.email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Provider getProvider() {
|
||||
return EmailProvider.embedded;
|
||||
}
|
||||
|
||||
protected void doSend(EmailTemplateParsed template, List<String> sendTo) throws MessagingException, UnsupportedEncodingException {
|
||||
MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
|
||||
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
|
||||
|
||||
helper.setFrom(this.sender);
|
||||
helper.setTo(sendTo.toArray(new String[0]));
|
||||
helper.setSubject(template.getSubject());
|
||||
helper.setText(new String(template.getText().getBytes(), StandardCharsets.UTF_8), true);
|
||||
|
||||
Set<Map.Entry<String, String>> entries = template.getAttachments().entrySet();
|
||||
for (Map.Entry<String, String> entry : entries) {
|
||||
helper.addAttachment(MimeUtility.encodeText(entry.getKey()), convertResource(entry.getValue()));
|
||||
}
|
||||
Set<Map.Entry<String, String>> imageEntries = template.getImages().entrySet();
|
||||
for (Map.Entry<String, String> image : imageEntries) {
|
||||
helper.addInline(image.getKey(), convertResource(image.getValue()), MediaType.APPLICATION_OCTET_STREAM_VALUE);
|
||||
}
|
||||
this.javaMailSender.send(mimeMessage);
|
||||
}
|
||||
|
||||
|
||||
protected InputStreamSource convertResource(String resource) {
|
||||
if (resource.startsWith("http")) {
|
||||
// Mono<Resource> resourceMono =
|
||||
// InputStream is = new FileInputStream();
|
||||
// InputStreamSource iss = new InputStreamResource(is, null);
|
||||
// TODO 使用其他工具实现,不用反应式框架
|
||||
WebClient.create()
|
||||
.get()
|
||||
.uri(resource)
|
||||
.accept(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.exchange()
|
||||
.flatMap(rep -> rep.bodyToMono(Resource.class))
|
||||
;
|
||||
} else {
|
||||
try {
|
||||
return new InputStreamResource(new FileInputStream(resource));
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
protected EmailTemplateParsed convert(EmailTemplate template, Map<String, Object> context) {
|
||||
String subject = template.getSubject();
|
||||
String text = template.getText();
|
||||
if (StringUtils.isEmpty(subject) || StringUtils.isEmpty(text)) {
|
||||
throw new BusinessException("模板内容错误,text 或者 subject 不能为空.");
|
||||
}
|
||||
String sendText = render(text, context);
|
||||
List<EmailTemplate.Attachment> tempAttachments = template.getAttachments();
|
||||
Map<String, String> attachments = new HashMap<>();
|
||||
if (tempAttachments != null) {
|
||||
for (EmailTemplate.Attachment tempAttachment : tempAttachments) {
|
||||
attachments.put(tempAttachment.getName(), render(tempAttachment.getLocation(), context));
|
||||
}
|
||||
}
|
||||
return EmailTemplateParsed.builder()
|
||||
.attachments(attachments)
|
||||
.images(extractSendTextImage(sendText))
|
||||
.text(sendText)
|
||||
.subject(render(subject, context))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
private Map<String, String> extractSendTextImage(String sendText) {
|
||||
Map<String, String> images = new HashMap<>();
|
||||
Document doc = Jsoup.parse(sendText);
|
||||
for (Element src : doc.getElementsByTag("img")) {
|
||||
String s = src.attr("src");
|
||||
if (s.startsWith("http")) {
|
||||
continue;
|
||||
}
|
||||
String tempKey = IDGenerator.MD5.generate();
|
||||
src.attr("src", "cid:".concat(tempKey));
|
||||
images.put(tempKey, s);
|
||||
}
|
||||
return images;
|
||||
}
|
||||
|
||||
private String render(String str, Map<String, Object> context) {
|
||||
return ExpressionUtils.analytical(str, context, "spel");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
package com.simaek.notify.email.embedded;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.cicdi.notify.*;
|
||||
import com.cicdi.notify.template.TemplateManager;
|
||||
import com.cicdi.notify.template.TemplateProperties;
|
||||
import com.cicdi.notify.template.TemplateProvider;
|
||||
import com.simaek.notify.email.EmailProvider;
|
||||
import com.simaek.notify.email.EmailTemplate;
|
||||
|
||||
public class DefaultEmailNotifierProvider implements NotifierProvider, TemplateProvider {
|
||||
|
||||
private final TemplateManager templateManager;
|
||||
|
||||
public DefaultEmailNotifierProvider(TemplateManager templateManager) {
|
||||
this.templateManager = templateManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NotifyType getType() {
|
||||
return DefaultNotifyType.email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Provider getProvider() {
|
||||
return EmailProvider.embedded;
|
||||
}
|
||||
|
||||
// public static final DefaultConfigMetadata templateConfig;
|
||||
//
|
||||
// public static final DefaultConfigMetadata notifierConfig;
|
||||
//
|
||||
// static {
|
||||
// {
|
||||
// SimplePropertyMetadata name = new SimplePropertyMetadata();
|
||||
// name.setId("name");
|
||||
// name.setName("文件名");
|
||||
// name.setValueType(new StringType());
|
||||
//
|
||||
// SimplePropertyMetadata location = new SimplePropertyMetadata();
|
||||
// location.setId("location");
|
||||
// location.setName("文件地址");
|
||||
// location.setValueType(new FileType()
|
||||
// .bodyType(FileType.BodyType.url)
|
||||
// .expand(allowInput.value(true)));
|
||||
//
|
||||
// templateConfig = new DefaultConfigMetadata("邮件模版", "")
|
||||
// .add("subject", "标题", "标题,可使用变量", new StringType().expand(maxLength.value(255L)))
|
||||
// .add("text", "内容", "", new StringType().expand(maxLength.value(5120L), isRichText.value(true)))
|
||||
// .add("sendTo", "收件人", "", new ArrayType().elementType(new StringType()))
|
||||
// .add("attachments", "附件列表", "", new ArrayType()
|
||||
// .elementType(new ObjectType()
|
||||
// .addPropertyMetadata(name)
|
||||
// .addPropertyMetadata(location)));
|
||||
// }
|
||||
//
|
||||
// {
|
||||
// SimplePropertyMetadata name = new SimplePropertyMetadata();
|
||||
// name.setId("name");
|
||||
// name.setName("配置名称");
|
||||
// name.setValueType(new StringType());
|
||||
//
|
||||
// SimplePropertyMetadata value = new SimplePropertyMetadata();
|
||||
// value.setId("value");
|
||||
// value.setName("配置值");
|
||||
// value.setValueType(new StringType());
|
||||
//
|
||||
// SimplePropertyMetadata description = new SimplePropertyMetadata();
|
||||
// description.setId("description");
|
||||
// description.setName("说明");
|
||||
// description.setValueType(new StringType());
|
||||
//
|
||||
// notifierConfig = new DefaultConfigMetadata("邮件配置", "")
|
||||
// .add("host", "服务器地址", "例如: pop3.qq.com", new StringType().expand(maxLength.value(255L)))
|
||||
// .add("port", "端口", "", new IntType().min(0).max(65536))
|
||||
// .add("sender", "发件人", "默认和用户名相同", new StringType())
|
||||
// .add("username", "用户名", "", new StringType())
|
||||
// .add("password", "密码", "", new PasswordType())
|
||||
// .add("properties", "其他配置", "", new ArrayType()
|
||||
// .elementType(new ObjectType()
|
||||
// .addPropertyMetadata(name)
|
||||
// .addPropertyMetadata(value)
|
||||
// .addPropertyMetadata(description)));
|
||||
// }
|
||||
//
|
||||
//
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public ConfigMetadata getNotifierConfigMetadata() {
|
||||
// return notifierConfig;
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public ConfigMetadata getTemplateConfigMetadata() {
|
||||
// return templateConfig;
|
||||
// }
|
||||
|
||||
@Override
|
||||
public DefaultEmailNotifier createNotifier(NotifierProperties properties) {
|
||||
return new DefaultEmailNotifier(properties, templateManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public EmailTemplate createTemplate(TemplateProperties properties) {
|
||||
return JSON.parseObject(properties.getTemplate(), EmailTemplate.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
package com.simaek.notify.email.embedded;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
public class DefaultEmailProperties {
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private String sender;
|
||||
|
||||
private List<ConfigProperty> properties;
|
||||
|
||||
public static class ConfigProperty {
|
||||
|
||||
private String name;
|
||||
|
||||
private String value;
|
||||
|
||||
private String description;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
}
|
||||
|
||||
public Properties createJavaMailProperties() {
|
||||
Properties properties = new Properties();
|
||||
if (this.properties != null) {
|
||||
for (ConfigProperty property : this.properties) {
|
||||
properties.put(property.getName(), property.getValue());
|
||||
}
|
||||
}
|
||||
return properties;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(String sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public List<ConfigProperty> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public void setProperties(List<ConfigProperty> properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
package com.cicdi.notify.email;
|
||||
|
||||
import com.cicdi.notify.*;
|
||||
import com.cicdi.notify.template.AbstractTemplateManager;
|
||||
import com.cicdi.notify.template.Template;
|
||||
import com.cicdi.notify.template.TemplateManager;
|
||||
import com.cicdi.notify.template.TemplateProperties;
|
||||
import com.simaek.notify.email.EmailProvider;
|
||||
import com.simaek.notify.email.embedded.DefaultEmailNotifierProvider;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author xueye
|
||||
*/
|
||||
public class DefaultEmailTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testSend() {
|
||||
// 通知器配置管理器
|
||||
NotifyConfigManager notifyConfigManager = new NotifyConfigManager() {
|
||||
@Override
|
||||
public NotifierProperties getNotifyConfig(NotifyType notifyType, String configId) {
|
||||
NotifierProperties notifierProperties = new NotifierProperties();
|
||||
notifierProperties.setType(DefaultNotifyType.email.getId());
|
||||
notifierProperties.setProvider(EmailProvider.embedded.getId());
|
||||
notifierProperties.setId("12");
|
||||
|
||||
Map<String, Object> config = new HashMap<>();
|
||||
|
||||
config.put("host", "nas.xueye.io");
|
||||
config.put("port", 587);
|
||||
config.put("username", "xueye");
|
||||
config.put("password", "snow@PX1314");
|
||||
config.put("sender", "master@simaek.com");
|
||||
Map<String, Object> p1 = new HashMap<>();
|
||||
p1.put("name", "mail.smtp.starttls.enable");
|
||||
p1.put("value", "true");
|
||||
Map<String, Object> p2 = new HashMap<>();
|
||||
p2.put("name", "mail.smtp.auth");
|
||||
p2.put("value", "true");
|
||||
config.put("properties", Arrays.asList(p1, p2));
|
||||
notifierProperties.setConfiguration(config);
|
||||
return notifierProperties;
|
||||
}
|
||||
};
|
||||
|
||||
// 模板管理器
|
||||
TemplateManager templateManager = new AbstractTemplateManager() {
|
||||
@Override
|
||||
protected TemplateProperties getProperties(NotifyType type, String id) {
|
||||
TemplateProperties templateProperties = new TemplateProperties();
|
||||
templateProperties.setType(DefaultNotifyType.email.getId());
|
||||
templateProperties.setProvider(EmailProvider.embedded.getId());
|
||||
templateProperties.setTemplate("{\"subject\":\"Hello\",\"sendTo\":[\"xueye404@qq.com\"],\"text\":\"<p>This is a test</p>\",\"attachments\":[]}");
|
||||
return templateProperties;
|
||||
}
|
||||
};
|
||||
|
||||
NotifierManager notifierManager = new DefaultNotifierManager(notifyConfigManager);
|
||||
DefaultEmailNotifierProvider defaultEmailNotifierProvider = new DefaultEmailNotifierProvider(templateManager);
|
||||
// register
|
||||
notifierManager.registerProvider(defaultEmailNotifierProvider);
|
||||
templateManager.register(defaultEmailNotifierProvider);
|
||||
|
||||
Notifier<Template> notifier = notifierManager.getNotifier(DefaultNotifyType.email, "123");
|
||||
notifier.send(templateManager.getTemplate(DefaultNotifyType.email, "124"), new HashMap<>());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user