完成nutzboot-starter-email

This commit is contained in:
黄川 2018-08-10 15:19:17 +08:00
parent 21d374b511
commit 057927a1d9
7 changed files with 77 additions and 74 deletions

View File

@ -2,6 +2,7 @@ package io.nutz.demo.simple;
import org.apache.commons.mail.ImageHtmlEmail;
import org.nutz.boot.NbApp;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
@ -12,8 +13,9 @@ public class MainLauncher {
private static final Log log = Logs.get();
@Inject
ImageHtmlEmail email;
@Inject("refer:$ioc")
protected Ioc ioc;
public static void main(String[] args) throws Exception {
new NbApp().setPrintProcDoc(true).run();
@ -21,14 +23,16 @@ public class MainLauncher {
public void init() {
try {
//注意垃圾收件箱
email.setSubject("NutzBootEmainStarter");
//ImageHtmlEmail 请不要使用注入模式每次使用都需要去Ioc取一下
ImageHtmlEmail htmlEmail = ioc.get(ImageHtmlEmail.class);
htmlEmail.setSubject("NutzBootEmainStarter");
//请使用自己的邮箱
email.addTo("自己的邮箱");
email.setHtmlMsg("此邮件是Nutz Boot Email Starter发送给您的测试邮件");
htmlEmail.addTo("自己的邮箱");
htmlEmail.setHtmlMsg("此邮件是Nutz Boot Email Starter发送给您的测试邮件");
log.debug("开始邮件发送");
email.send();
log.debug("邮件发送完毕");
htmlEmail.buildMimeMessage();
htmlEmail.sendMimeMessage();
log.debug("邮件发送完毕-请查收!注意有可能进入垃圾收件箱");
} catch (Exception e) {
log.error(e.getMessage(), e);
}

View File

@ -0,0 +1,10 @@
package org.nutz.boot.starter.email;
import org.nutz.ioc.loader.json.JsonLoader;
public class EmailIocLoader extends JsonLoader {
public EmailIocLoader() {
super("org/nutz/boot/starter/email/email.js");
}
}

View File

@ -0,0 +1,36 @@
package org.nutz.boot.starter.email;
import org.nutz.boot.annotation.PropDoc;
import org.nutz.boot.ioc.IocLoaderProvider;
import org.nutz.ioc.IocLoader;
public class EmailIocLoaderStarter implements IocLoaderProvider {
protected static final String PRE = "email.";
@PropDoc(group = "email", value = "email的ip或域名地址", need = true)
public static final String PROP_HOSTNAME = PRE + "HostName";
@PropDoc(group = "email", value = "email的SmtpPort端口", need = true)
public static final String PROP_SMTPPORT = PRE + "SmtpPort";
@PropDoc(group = "email", value = "email的用户名", need = true)
public static final String PROP_USERNAME = PRE + "UserName";
@PropDoc(group = "email", value = "email的密码", need = true)
public static final String PROP_PASSWORD = PRE + "Password";
@PropDoc(group = "email", value = "email开启SSL连接", defaultValue = "true", type = "boolean")
public static final String PROP_SSLONCONNECT = PRE + "SSLOnConnect";
@PropDoc(group = "email", value = "email的写信人", need = true)
public static final String PROP_FROM = PRE + "From";
@PropDoc(group = "email", value = "email的编码", defaultValue = "UTF-8")
public static final String PROP_CHARSET = PRE + "charset";
@Override
public IocLoader getIocLoader() {
return new EmailIocLoader();
}
}

View File

@ -1,65 +0,0 @@
package org.nutz.boot.starter.email;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.ImageHtmlEmail;
import org.nutz.boot.AppContext;
import org.nutz.boot.annotation.PropDoc;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.impl.PropertiesProxy;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
@IocBean
public class EmailStarter {
@Inject("refer:$ioc")
protected Ioc ioc;
@Inject
protected PropertiesProxy conf;
@Inject
protected AppContext appContext;
protected static final String PRE = "email.";
@PropDoc(group = "email", value = "email的ip或域名地址", need = true)
public static final String PROP_HOSTNAME = PRE + "HostName";
@PropDoc(group = "email", value = "email的SmtpPort端口", need = true)
public static final String PROP_SMTPPORT = PRE + "SmtpPort";
@PropDoc(group = "email", value = "email的用户名", need = true)
public static final String PROP_USERNAME = PRE + "UserName";
@PropDoc(group = "email", value = "email的密码", need = true)
public static final String PROP_PASSWORD = PRE + "Password";
@PropDoc(group = "email", value = "email开启SSL连接", defaultValue = "true", type = "boolean")
public static final String PROP_SSLONCONNECT = PRE + "SSLOnConnect";
@PropDoc(group = "email", value = "email的写信人", need = true)
public static final String PROP_FROM = PRE + "From";
@PropDoc(group = "email", value = "email的编码", defaultValue = "utf-8")
public static final String PROP_CHARSET = PRE + "charset";
@IocBean(name = "emailAuthenticator")
public DefaultAuthenticator createDefaultAuthenticator() {
return new DefaultAuthenticator(conf.get(PROP_USERNAME, ""), conf.get(PROP_PASSWORD, ""));
}
@IocBean(name = "htmlEmail", singleton = false)
public ImageHtmlEmail getImageHtmlEmail() throws EmailException {
ImageHtmlEmail imageHtmlEmail = new ImageHtmlEmail();
imageHtmlEmail.setAuthenticator(createDefaultAuthenticator());
imageHtmlEmail.setCharset(conf.get(PROP_CHARSET, "utf-8"));
imageHtmlEmail.setHostName(conf.get(PROP_HOSTNAME));
imageHtmlEmail.setSmtpPort(conf.getInt(PROP_SMTPPORT));
imageHtmlEmail.setFrom(conf.get(PROP_FROM));
imageHtmlEmail.setSSLOnConnect(conf.getBoolean(PROP_SSLONCONNECT));
return imageHtmlEmail;
}
}

View File

@ -0,0 +1 @@
org.nutz.boot.starter.email.EmailIocLoaderStarter

View File

@ -0,0 +1,18 @@
var ioc = {
emailAuthenticator: {
type: "org.apache.commons.mail.DefaultAuthenticator",
args: [{java: "$conf.get('email.UserName')"}, {java: "$conf.get('email.Password')"}]
},
imageHtmlEmail: {
type: "org.apache.commons.mail.ImageHtmlEmail",
singleton: false,
fields: {
hostName: {java: "$conf.get('email.HostName')"},
smtpPort: {java: "$conf.get('email.SmtpPort')"},
authenticator: {refer: "emailAuthenticator"},
SSLOnConnect: {java: "$conf.get('email.SSLOnConnect')"},
from: {java: "$conf.get('email.From')"},
charset: {java: "$conf.get('email.charset')"}
}
}
};