|
|
|
@ -12,6 +12,7 @@ import cn.estsh.impp.framework.boot.util.RedisCacheTool;
|
|
|
|
|
import com.sun.mail.smtp.SMTPAddressFailedException;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.activation.DataHandler;
|
|
|
|
@ -38,6 +39,12 @@ import java.util.Properties;
|
|
|
|
|
public class MailUtil {
|
|
|
|
|
public static final Logger LOGGER = LoggerFactory.getLogger(MailUtil.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 开启starttls , 不然有些邮箱会报认证失败的错误,如outlook
|
|
|
|
|
*/
|
|
|
|
|
@Value("${mail.smtp.starttls.enable:false}")
|
|
|
|
|
private boolean smtpStarttlsEnabled;
|
|
|
|
|
|
|
|
|
|
// 收件人
|
|
|
|
|
private Address[] to = new Address[]{};
|
|
|
|
|
private Address[] cc = new Address[]{};
|
|
|
|
@ -55,9 +62,9 @@ public class MailUtil {
|
|
|
|
|
private boolean isAuthenticationSMTP = false;
|
|
|
|
|
private final List<BodyPart> attachmentList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
private static final int MAIL_SMTP_TIMEOUT =10000;
|
|
|
|
|
private static final int MAIL_SMTP_CONNECTIONTIMEOUT =10000;
|
|
|
|
|
private static final int MAIL_SMTP_WRITETIMEOUT =10000;
|
|
|
|
|
private static final int MAIL_SMTP_TIMEOUT = 10000;
|
|
|
|
|
private static final int MAIL_SMTP_CONNECTIONTIMEOUT = 10000;
|
|
|
|
|
private static final int MAIL_SMTP_WRITETIMEOUT = 10000;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化服务器邮箱参数
|
|
|
|
@ -206,7 +213,8 @@ public class MailUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加附件
|
|
|
|
|
* @param fileName 附件名
|
|
|
|
|
*
|
|
|
|
|
* @param fileName 附件名
|
|
|
|
|
* @param attachmentByte
|
|
|
|
|
* @throws MessagingException
|
|
|
|
|
*/
|
|
|
|
@ -217,12 +225,13 @@ public class MailUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加附件
|
|
|
|
|
* @param fileName 附件名
|
|
|
|
|
*
|
|
|
|
|
* @param fileName 附件名
|
|
|
|
|
* @param attachmentByte
|
|
|
|
|
* @param contentType
|
|
|
|
|
* @throws MessagingException
|
|
|
|
|
*/
|
|
|
|
|
public void addAttachment(String fileName, byte[] attachmentByte,String contentType) throws MessagingException {
|
|
|
|
|
public void addAttachment(String fileName, byte[] attachmentByte, String contentType) throws MessagingException {
|
|
|
|
|
BodyPart attachmentBodyPart = new MimeBodyPart();
|
|
|
|
|
DataSource source = new ByteArrayDataSource(attachmentByte, contentType);
|
|
|
|
|
attachmentBodyPart.setDataHandler(new DataHandler(source));
|
|
|
|
@ -232,6 +241,7 @@ public class MailUtil {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加附件
|
|
|
|
|
*
|
|
|
|
|
* @param attachment 附件对象
|
|
|
|
|
* @throws MessagingException
|
|
|
|
|
*/
|
|
|
|
@ -243,7 +253,7 @@ public class MailUtil {
|
|
|
|
|
attachmentList.add(attachmentBodyPart);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cleanAttachmentList(){
|
|
|
|
|
public void cleanAttachmentList() {
|
|
|
|
|
attachmentList.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -267,6 +277,9 @@ public class MailUtil {
|
|
|
|
|
if (this.isAuthenticationSMTP) {
|
|
|
|
|
server.put("mail.smtp.auth", "true");
|
|
|
|
|
}
|
|
|
|
|
if (smtpStarttlsEnabled) {
|
|
|
|
|
server.put("mail.smtp.starttls.enable", "true");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Session conn = Session.getInstance(server, null);
|
|
|
|
|
|
|
|
|
@ -323,7 +336,7 @@ public class MailUtil {
|
|
|
|
|
LOGGER.info("邮件发送成功");
|
|
|
|
|
|
|
|
|
|
} catch (SMTPAddressFailedException e) {
|
|
|
|
|
LOGGER.error("邮件发送异常",e);
|
|
|
|
|
LOGGER.error("邮件发送异常", e);
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_RECIPIENT_NOT_EXIST.getCode())
|
|
|
|
@ -331,7 +344,7 @@ public class MailUtil {
|
|
|
|
|
.setErrorSolution("请重新设置收件人")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (AuthenticationFailedException e) {
|
|
|
|
|
LOGGER.error("邮件发送异常",e);
|
|
|
|
|
LOGGER.error("邮件发送异常", e);
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_LOGIN_PASS_ERROR.getCode())
|
|
|
|
@ -339,7 +352,7 @@ public class MailUtil {
|
|
|
|
|
.setErrorSolution("请重新设置账号或密码")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
|
LOGGER.error("邮件发送异常",e);
|
|
|
|
|
LOGGER.error("邮件发送异常", e);
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
|
|
|
|
@ -347,7 +360,7 @@ public class MailUtil {
|
|
|
|
|
.setErrorSolution("请重新设置邮件服务器地址或端口")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
LOGGER.error("邮件发送异常",e);
|
|
|
|
|
LOGGER.error("邮件发送异常", e);
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
|
|
|
|
@ -355,7 +368,7 @@ public class MailUtil {
|
|
|
|
|
.setErrorSolution("请重新设置邮件服务器地址或端口")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("邮件发送异常",e);
|
|
|
|
|
LOGGER.error("邮件发送异常", e);
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
|
|
|
|
|