|
|
|
@ -1,16 +1,24 @@
|
|
|
|
|
package cn.estsh.i3plus.core.apiservice.util;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
import com.sun.mail.smtp.SMTPAddressFailedException;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.mail.*;
|
|
|
|
|
import javax.mail.Address;
|
|
|
|
|
import javax.mail.AuthenticationFailedException;
|
|
|
|
|
import javax.mail.Message;
|
|
|
|
|
import javax.mail.MessagingException;
|
|
|
|
|
import javax.mail.Session;
|
|
|
|
|
import javax.mail.Transport;
|
|
|
|
|
import javax.mail.internet.AddressException;
|
|
|
|
|
import javax.mail.internet.InternetAddress;
|
|
|
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
@ -21,7 +29,7 @@ import java.util.Properties;
|
|
|
|
|
/**
|
|
|
|
|
* @Description : 邮件工具类
|
|
|
|
|
* @Reference :
|
|
|
|
|
* @Author : yunhao
|
|
|
|
|
* @Author : yunhaoo
|
|
|
|
|
* @CreateDate : 2018-11-14 19:24
|
|
|
|
|
* @Modify:
|
|
|
|
|
**/
|
|
|
|
@ -39,7 +47,7 @@ public class MailUtil {
|
|
|
|
|
private String content = "";
|
|
|
|
|
private String smtpHost = "";
|
|
|
|
|
private int smtpPort = 25;
|
|
|
|
|
private String content_type = "";
|
|
|
|
|
private String contentType = "";
|
|
|
|
|
|
|
|
|
|
private String smtpUser = "";
|
|
|
|
|
private String smtpPassword = "";
|
|
|
|
@ -48,37 +56,55 @@ public class MailUtil {
|
|
|
|
|
@Autowired
|
|
|
|
|
private ISysConfigService sysConfigService;
|
|
|
|
|
|
|
|
|
|
// 初始化服务器邮箱参数
|
|
|
|
|
/**
|
|
|
|
|
* 初始化服务器邮箱参数
|
|
|
|
|
* @return 初始化
|
|
|
|
|
*/
|
|
|
|
|
public MailUtil init() {
|
|
|
|
|
// 查询是否开启邮件服务器
|
|
|
|
|
SysConfig sysConfig = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_SWITCH);
|
|
|
|
|
if(sysConfig != null) {
|
|
|
|
|
this.mailSwitch = Integer.parseInt(sysConfig.getConfigValue());
|
|
|
|
|
this.smtpHost = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_HOST).getConfigValue();
|
|
|
|
|
this.smtpPort = Integer.parseInt(sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_PORT).getConfigValue());
|
|
|
|
|
this.from = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_USER).getConfigValue();
|
|
|
|
|
this.nick = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_NICK).getConfigValue();
|
|
|
|
|
this.smtpUser = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_USER).getConfigValue();
|
|
|
|
|
this.smtpPassword = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_PASSWORD).getConfigValue();
|
|
|
|
|
this.isAuthenticationSMTP = true;
|
|
|
|
|
} else{
|
|
|
|
|
this.mailSwitch = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();
|
|
|
|
|
try {
|
|
|
|
|
// 查询是否开启邮件服务器
|
|
|
|
|
SysConfig sysConfig = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_SWITCH);
|
|
|
|
|
if (sysConfig != null) {
|
|
|
|
|
this.mailSwitch = Integer.parseInt(sysConfig.getConfigValue());
|
|
|
|
|
this.smtpHost = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_HOST).getConfigValue();
|
|
|
|
|
this.smtpPort = Integer.parseInt(sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_PORT).getConfigValue());
|
|
|
|
|
this.from = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_USER).getConfigValue();
|
|
|
|
|
this.nick = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_NICK).getConfigValue();
|
|
|
|
|
this.smtpUser = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_USER).getConfigValue();
|
|
|
|
|
this.smtpPassword = sysConfigService.getSysConfigByCode(PlatformConstWords.MAIL_PASSWORD).getConfigValue();
|
|
|
|
|
this.isAuthenticationSMTP = true;
|
|
|
|
|
} else {
|
|
|
|
|
this.mailSwitch = CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue();
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
LOGGER.error("邮箱配置初始化失败!", e.getMessage());
|
|
|
|
|
return new MailUtil();
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化服务器邮箱参数
|
|
|
|
|
/**
|
|
|
|
|
* 初始化服务器邮箱参数
|
|
|
|
|
* @param mailConfig 邮箱配置信息
|
|
|
|
|
* @return 初始化
|
|
|
|
|
*/
|
|
|
|
|
public MailUtil init(MailConfig mailConfig) {
|
|
|
|
|
this.mailSwitch = mailConfig.getMailSwitch();
|
|
|
|
|
this.smtpHost = mailConfig.getMailHost();
|
|
|
|
|
this.smtpPort = mailConfig.getMailPort();
|
|
|
|
|
this.from = mailConfig.getMailUser();
|
|
|
|
|
this.nick = mailConfig.getMailNick();
|
|
|
|
|
this.smtpUser = mailConfig.getMailUser();
|
|
|
|
|
this.smtpPassword = mailConfig.getMailPassword();
|
|
|
|
|
this.isAuthenticationSMTP = true;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
try {
|
|
|
|
|
this.mailSwitch = mailConfig.getMailSwitch();
|
|
|
|
|
this.smtpHost = mailConfig.getMailHost();
|
|
|
|
|
this.smtpPort = mailConfig.getMailPort();
|
|
|
|
|
this.from = mailConfig.getMailUser();
|
|
|
|
|
this.nick = mailConfig.getMailNick();
|
|
|
|
|
this.smtpUser = mailConfig.getMailUser();
|
|
|
|
|
this.smtpPassword = mailConfig.getMailPassword();
|
|
|
|
|
this.isAuthenticationSMTP = true;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
} catch (Exception e){
|
|
|
|
|
LOGGER.error("邮箱配置初始化失败!", e.getMessage());
|
|
|
|
|
return new MailUtil();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -105,10 +131,10 @@ public class MailUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 设置多个收件人地址
|
|
|
|
|
*
|
|
|
|
|
* @param Emails 收件人Email地址
|
|
|
|
|
* @param emails 收件人Email地址
|
|
|
|
|
*/
|
|
|
|
|
public void setTo(String[] Emails) {
|
|
|
|
|
this.to = getAddress(Emails);
|
|
|
|
|
public void setTo(String[] emails) {
|
|
|
|
|
this.to = getAddress(emails);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -125,10 +151,10 @@ public class MailUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 设置多个抄送地址
|
|
|
|
|
*
|
|
|
|
|
* @param Emails 抄送地址
|
|
|
|
|
* @param emails 抄送地址
|
|
|
|
|
*/
|
|
|
|
|
public void setCC(String[] Emails) {
|
|
|
|
|
this.cc = getAddress(Emails);
|
|
|
|
|
public void setCC(String[] emails) {
|
|
|
|
|
this.cc = getAddress(emails);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -155,7 +181,7 @@ public class MailUtil {
|
|
|
|
|
* @param contentType 请从CommonConstWords.MAIL_MODE_TEXT和MAIL_MODE_HTML中选择
|
|
|
|
|
*/
|
|
|
|
|
public void setContentType(String contentType) {
|
|
|
|
|
this.content_type = contentType;
|
|
|
|
|
this.contentType = contentType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Address[] getAddress(String[] add) {
|
|
|
|
@ -173,7 +199,8 @@ public class MailUtil {
|
|
|
|
|
/**
|
|
|
|
|
* 发送邮件
|
|
|
|
|
*/
|
|
|
|
|
public void send() throws UnsupportedEncodingException, MessagingException {
|
|
|
|
|
public void send() {
|
|
|
|
|
try{
|
|
|
|
|
// 是否开启邮箱配置
|
|
|
|
|
if(this.mailSwitch != CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue()){
|
|
|
|
|
return;
|
|
|
|
@ -210,7 +237,7 @@ public class MailUtil {
|
|
|
|
|
// 消息主体内容 处理
|
|
|
|
|
this.content = this.content == null ? "" : this.content;
|
|
|
|
|
|
|
|
|
|
msg.setContent(this.content, this.content_type);
|
|
|
|
|
msg.setContent(this.content, this.contentType);
|
|
|
|
|
|
|
|
|
|
msg.saveChanges();
|
|
|
|
|
if (this.isAuthenticationSMTP) {
|
|
|
|
@ -222,5 +249,39 @@ public class MailUtil {
|
|
|
|
|
Transport.send(msg, msg.getAllRecipients());
|
|
|
|
|
}
|
|
|
|
|
LOGGER.info("邮件发送成功");
|
|
|
|
|
|
|
|
|
|
} catch (SMTPAddressFailedException e){
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_RECIPIENT_NOT_EXIST.getCode())
|
|
|
|
|
.setErrorDetail(ImppExceptionEnum.MAIL_RECIPIENT_NOT_EXIST.getDescription())
|
|
|
|
|
.setErrorSolution("请重新设置收件人")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (AuthenticationFailedException e){
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_LOGIN_PASS_ERROR.getCode())
|
|
|
|
|
.setErrorDetail(ImppExceptionEnum.MAIL_LOGIN_PASS_ERROR.getDescription())
|
|
|
|
|
.setErrorSolution("请重新设置账号或密码")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (MessagingException e){
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
|
|
|
|
|
.setErrorDetail(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getDescription())
|
|
|
|
|
.setErrorSolution("请重新设置邮件服务器地址或端口")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.CORE.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
|
|
|
|
|
.setErrorDetail(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getDescription())
|
|
|
|
|
.setErrorSolution("请重新设置邮件服务器地址或端口")
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|