mail工具类增加starttls的配置

yun-zuoyi
nies 4 years ago
parent e4aa9c48f2
commit aa396d1ac5

@ -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())

Loading…
Cancel
Save