|
|
|
@ -4,7 +4,6 @@ import cn.estsh.i3plus.core.api.iservice.busi.ISysConfigService;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
|
|
|
|
|
import cn.estsh.i3plus.pojo.base.enumutil.ImppEnumUtil;
|
|
|
|
|
import cn.estsh.i3plus.pojo.platform.bean.MailConfig;
|
|
|
|
|
import cn.estsh.i3plus.pojo.platform.bean.SysConfig;
|
|
|
|
|
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
|
|
|
|
@ -38,8 +37,8 @@ public class MailUtil {
|
|
|
|
|
public static final Logger LOGGER = LoggerFactory.getLogger(MailUtil.class);
|
|
|
|
|
|
|
|
|
|
// 收件人
|
|
|
|
|
private Address[] to = null;
|
|
|
|
|
private Address[] cc = null;
|
|
|
|
|
private Address[] to = new Address[]{};
|
|
|
|
|
private Address[] cc = new Address[]{};
|
|
|
|
|
private int mailSwitch = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();
|
|
|
|
|
private String from = "";
|
|
|
|
|
private String nick = "";
|
|
|
|
@ -58,6 +57,7 @@ public class MailUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化服务器邮箱参数
|
|
|
|
|
*
|
|
|
|
|
* @return 初始化
|
|
|
|
|
*/
|
|
|
|
|
public MailUtil init() {
|
|
|
|
@ -77,7 +77,7 @@ public class MailUtil {
|
|
|
|
|
this.mailSwitch = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("邮箱配置初始化失败!", e.getMessage());
|
|
|
|
|
return new MailUtil();
|
|
|
|
|
}
|
|
|
|
@ -86,6 +86,7 @@ public class MailUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化服务器邮箱参数
|
|
|
|
|
*
|
|
|
|
|
* @param mailConfig 邮箱配置信息
|
|
|
|
|
* @return 初始化
|
|
|
|
|
*/
|
|
|
|
@ -101,7 +102,7 @@ public class MailUtil {
|
|
|
|
|
this.isAuthenticationSMTP = true;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("邮箱配置初始化失败!", e.getMessage());
|
|
|
|
|
return new MailUtil();
|
|
|
|
|
}
|
|
|
|
@ -184,13 +185,18 @@ public class MailUtil {
|
|
|
|
|
this.contentType = contentType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取邮箱地址
|
|
|
|
|
* @param add 邮箱地址字符串
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private Address[] getAddress(String[] add) {
|
|
|
|
|
Address[] a = new Address[add.length];
|
|
|
|
|
for (int i = 0; i < add.length; i++) {
|
|
|
|
|
try {
|
|
|
|
|
a[i] = new InternetAddress(add[i]);
|
|
|
|
|
} catch (AddressException ex) {
|
|
|
|
|
LOGGER.error("邮箱地址错误!",ex.getMessage());
|
|
|
|
|
LOGGER.error("【" + add[i] + "】邮箱地址错误!", ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return a;
|
|
|
|
@ -200,57 +206,57 @@ public class MailUtil {
|
|
|
|
|
* 发送邮件
|
|
|
|
|
*/
|
|
|
|
|
public void send() {
|
|
|
|
|
try{
|
|
|
|
|
// 是否开启邮箱配置
|
|
|
|
|
if(this.mailSwitch != CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Properties server = new Properties();
|
|
|
|
|
server.put("mail.smtp.port", String.valueOf(this.smtpPort));
|
|
|
|
|
server.put("mail.smtp.host", this.smtpHost);
|
|
|
|
|
if (this.isAuthenticationSMTP) {
|
|
|
|
|
server.put("mail.smtp.auth", "true");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// 是否开启邮箱配置
|
|
|
|
|
if (this.mailSwitch != CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Properties server = new Properties();
|
|
|
|
|
server.put("mail.smtp.port", String.valueOf(this.smtpPort));
|
|
|
|
|
server.put("mail.smtp.host", this.smtpHost);
|
|
|
|
|
if (this.isAuthenticationSMTP) {
|
|
|
|
|
server.put("mail.smtp.auth", "true");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Session conn = Session.getInstance(server, null);
|
|
|
|
|
Session conn = Session.getInstance(server, null);
|
|
|
|
|
|
|
|
|
|
MimeMessage msg = new MimeMessage(conn);
|
|
|
|
|
if (nick != null && !"".equals(nick)) {
|
|
|
|
|
nick = MimeUtility.encodeText(nick, MimeUtility.mimeCharset("gb2312"), null);
|
|
|
|
|
msg.setSubject(nick);
|
|
|
|
|
msg.setFrom(new InternetAddress(nick + " <" + from + ">"));
|
|
|
|
|
} else {
|
|
|
|
|
msg.setFrom(new InternetAddress(this.from));
|
|
|
|
|
}
|
|
|
|
|
// 收件人
|
|
|
|
|
if (this.to != null) {
|
|
|
|
|
msg.setRecipients(Message.RecipientType.TO, this.to);
|
|
|
|
|
}
|
|
|
|
|
// 抄送
|
|
|
|
|
if (this.cc != null) {
|
|
|
|
|
msg.setRecipients(Message.RecipientType.CC, this.cc);
|
|
|
|
|
}
|
|
|
|
|
MimeMessage msg = new MimeMessage(conn);
|
|
|
|
|
if (nick != null && !"".equals(nick)) {
|
|
|
|
|
nick = MimeUtility.encodeText(nick, MimeUtility.mimeCharset("gb2312"), null);
|
|
|
|
|
msg.setSubject(nick);
|
|
|
|
|
msg.setFrom(new InternetAddress(nick + " <" + from + ">"));
|
|
|
|
|
} else {
|
|
|
|
|
msg.setFrom(new InternetAddress(this.from));
|
|
|
|
|
}
|
|
|
|
|
// 收件人
|
|
|
|
|
if (this.to != null) {
|
|
|
|
|
msg.setRecipients(Message.RecipientType.TO, this.to);
|
|
|
|
|
}
|
|
|
|
|
// 抄送
|
|
|
|
|
if (this.cc != null) {
|
|
|
|
|
msg.setRecipients(Message.RecipientType.CC, this.cc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg.setSubject(MimeUtility.encodeText(this.title, MimeUtility.mimeCharset("gb2312"), null));
|
|
|
|
|
msg.setSubject(this.title);
|
|
|
|
|
msg.setSubject(MimeUtility.encodeText(this.title, MimeUtility.mimeCharset("gb2312"), null));
|
|
|
|
|
msg.setSubject(this.title);
|
|
|
|
|
|
|
|
|
|
// 消息主体内容 处理
|
|
|
|
|
this.content = this.content == null ? "" : this.content;
|
|
|
|
|
// 消息主体内容 处理
|
|
|
|
|
this.content = this.content == null ? "" : this.content;
|
|
|
|
|
|
|
|
|
|
msg.setContent(this.content, this.contentType);
|
|
|
|
|
msg.setContent(this.content, this.contentType);
|
|
|
|
|
|
|
|
|
|
msg.saveChanges();
|
|
|
|
|
if (this.isAuthenticationSMTP) {
|
|
|
|
|
Transport transport = conn.getTransport("smtp");
|
|
|
|
|
transport.connect(this.smtpHost, this.smtpUser, this.smtpPassword);
|
|
|
|
|
transport.sendMessage(msg, msg.getAllRecipients());
|
|
|
|
|
transport.close();
|
|
|
|
|
} else {
|
|
|
|
|
Transport.send(msg, msg.getAllRecipients());
|
|
|
|
|
}
|
|
|
|
|
LOGGER.info("邮件发送成功");
|
|
|
|
|
msg.saveChanges();
|
|
|
|
|
if (this.isAuthenticationSMTP) {
|
|
|
|
|
Transport transport = conn.getTransport("smtp");
|
|
|
|
|
transport.connect(this.smtpHost, this.smtpUser, this.smtpPassword);
|
|
|
|
|
transport.sendMessage(msg, msg.getAllRecipients());
|
|
|
|
|
transport.close();
|
|
|
|
|
} else {
|
|
|
|
|
Transport.send(msg, msg.getAllRecipients());
|
|
|
|
|
}
|
|
|
|
|
LOGGER.info("邮件发送成功");
|
|
|
|
|
|
|
|
|
|
} catch (SMTPAddressFailedException e){
|
|
|
|
|
} catch (SMTPAddressFailedException e) {
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
@ -258,7 +264,7 @@ public class MailUtil {
|
|
|
|
|
.setErrorDetail(ImppExceptionEnum.MAIL_RECIPIENT_NOT_EXIST.getDescription())
|
|
|
|
|
.setErrorSolution("请重新设置收件人")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (AuthenticationFailedException e){
|
|
|
|
|
} catch (AuthenticationFailedException e) {
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
@ -266,7 +272,7 @@ public class MailUtil {
|
|
|
|
|
.setErrorDetail(ImppExceptionEnum.MAIL_LOGIN_PASS_ERROR.getDescription())
|
|
|
|
|
.setErrorSolution("请重新设置账号或密码")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (MessagingException e){
|
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|