|
|
|
@ -1,9 +1,12 @@
|
|
|
|
|
package cn.estsh.i3plus.core.apiservice.util;
|
|
|
|
|
|
|
|
|
|
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.tool.StringTool;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
|
|
|
|
|
import cn.estsh.i3plus.platform.common.util.FileContentTypeTool;
|
|
|
|
|
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.impp.framework.boot.exception.ImppExceptionBuilder;
|
|
|
|
|
import cn.estsh.impp.framework.boot.util.ImppRedis;
|
|
|
|
@ -12,18 +15,19 @@ import com.sun.mail.smtp.SMTPAddressFailedException;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import javax.activation.DataHandler;
|
|
|
|
|
import javax.activation.DataSource;
|
|
|
|
|
import javax.activation.FileDataSource;
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
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;
|
|
|
|
|
import javax.mail.internet.MimeUtility;
|
|
|
|
|
import javax.mail.*;
|
|
|
|
|
import javax.mail.internet.*;
|
|
|
|
|
import javax.mail.util.ByteArrayDataSource;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -52,6 +56,7 @@ public class MailUtil {
|
|
|
|
|
private String smtpUser = "";
|
|
|
|
|
private String smtpPassword = "";
|
|
|
|
|
private boolean isAuthenticationSMTP = false;
|
|
|
|
|
private final List<BodyPart> attachmentList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
@Resource(name = CommonConstWords.IMPP_REDIS_RES)
|
|
|
|
|
private ImppRedis redisRes;
|
|
|
|
@ -73,6 +78,7 @@ public class MailUtil {
|
|
|
|
|
this.smtpUser = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.MAIL_USER);
|
|
|
|
|
this.smtpPassword = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.MAIL_PASSWORD);
|
|
|
|
|
this.isAuthenticationSMTP = true;
|
|
|
|
|
this.attachmentList.clear();
|
|
|
|
|
return this;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error("邮箱配置初始化失败!", e.getMessage());
|
|
|
|
@ -201,6 +207,49 @@ public class MailUtil {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加附件
|
|
|
|
|
* @param fileName 附件名
|
|
|
|
|
* @param attachmentByte
|
|
|
|
|
* @throws MessagingException
|
|
|
|
|
*/
|
|
|
|
|
public void addAttachment(String fileName, byte[] attachmentByte) throws MessagingException {
|
|
|
|
|
addAttachment(fileName, attachmentByte,
|
|
|
|
|
FileContentTypeTool.getContentType(StringTool.getStringFileSuffix(fileName, true)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加附件
|
|
|
|
|
* @param fileName 附件名
|
|
|
|
|
* @param attachmentByte
|
|
|
|
|
* @param 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));
|
|
|
|
|
attachmentBodyPart.setFileName(fileName);
|
|
|
|
|
attachmentList.add(attachmentBodyPart);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 添加附件
|
|
|
|
|
* @param attachment 附件对象
|
|
|
|
|
* @throws MessagingException
|
|
|
|
|
*/
|
|
|
|
|
public void addAttachment(File attachment) throws MessagingException {
|
|
|
|
|
BodyPart attachmentBodyPart = new MimeBodyPart();
|
|
|
|
|
DataSource source = new FileDataSource(attachment);
|
|
|
|
|
attachmentBodyPart.setDataHandler(new DataHandler(source));
|
|
|
|
|
attachmentBodyPart.setFileName(attachment.getName());
|
|
|
|
|
attachmentList.add(attachmentBodyPart);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void cleanAttachmentList(){
|
|
|
|
|
attachmentList.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送邮件
|
|
|
|
|
*/
|
|
|
|
|
public void send() {
|
|
|
|
@ -241,7 +290,23 @@ public class MailUtil {
|
|
|
|
|
// 消息主体内容 处理
|
|
|
|
|
this.content = this.content == null ? "" : this.content;
|
|
|
|
|
|
|
|
|
|
msg.setContent(this.content, this.contentType);
|
|
|
|
|
// 判断是否存在附件
|
|
|
|
|
if (!attachmentList.isEmpty()) {
|
|
|
|
|
// 添加邮件的各个部分内容,包括文本内容和附件
|
|
|
|
|
Multipart multipart = new MimeMultipart();
|
|
|
|
|
|
|
|
|
|
// 添加邮件正文
|
|
|
|
|
BodyPart contentPart = new MimeBodyPart();
|
|
|
|
|
contentPart.setContent(content, this.contentType);
|
|
|
|
|
multipart.addBodyPart(contentPart);
|
|
|
|
|
|
|
|
|
|
for (BodyPart bodyPart : attachmentList) {
|
|
|
|
|
multipart.addBodyPart(bodyPart);
|
|
|
|
|
}
|
|
|
|
|
msg.setContent(multipart);
|
|
|
|
|
} else {
|
|
|
|
|
msg.setContent(this.content, this.contentType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg.saveChanges();
|
|
|
|
|
if (this.isAuthenticationSMTP) {
|
|
|
|
@ -255,7 +320,7 @@ public class MailUtil {
|
|
|
|
|
LOGGER.info("邮件发送成功");
|
|
|
|
|
|
|
|
|
|
} catch (SMTPAddressFailedException e) {
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
LOGGER.error("邮件发送异常",e);
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_RECIPIENT_NOT_EXIST.getCode())
|
|
|
|
@ -263,7 +328,7 @@ public class MailUtil {
|
|
|
|
|
.setErrorSolution("请重新设置收件人")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (AuthenticationFailedException e) {
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
LOGGER.error("邮件发送异常",e);
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_LOGIN_PASS_ERROR.getCode())
|
|
|
|
@ -271,7 +336,7 @@ public class MailUtil {
|
|
|
|
|
.setErrorSolution("请重新设置账号或密码")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
LOGGER.error("邮件发送异常",e);
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
|
|
|
|
@ -279,7 +344,7 @@ public class MailUtil {
|
|
|
|
|
.setErrorSolution("请重新设置邮件服务器地址或端口")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
LOGGER.error("邮件发送异常",e);
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
|
|
|
|
@ -287,7 +352,7 @@ public class MailUtil {
|
|
|
|
|
.setErrorSolution("请重新设置邮件服务器地址或端口")
|
|
|
|
|
.build();
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LOGGER.error(e.getClass() + "\t" + e.getMessage());
|
|
|
|
|
LOGGER.error("邮件发送异常",e);
|
|
|
|
|
throw ImppExceptionBuilder.newInstance()
|
|
|
|
|
.setSystemID(CommonEnumUtil.SOFT_TYPE.IMPP.getCode())
|
|
|
|
|
.setErrorCode(ImppExceptionEnum.MAIL_SERVER_CONFIG_ERROR.getCode())
|
|
|
|
@ -296,4 +361,25 @@ public class MailUtil {
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException, MessagingException {
|
|
|
|
|
MailConfig mailConfig = new MailConfig();
|
|
|
|
|
mailConfig.setMailSwitch(1);
|
|
|
|
|
mailConfig.setMailHost("smtphm.qiye.163.com");
|
|
|
|
|
mailConfig.setMailPort(25);
|
|
|
|
|
mailConfig.setMailNick("impp-test@estsh.com");
|
|
|
|
|
mailConfig.setMailUser("impp-test@estsh.com");
|
|
|
|
|
mailConfig.setMailPassword("Aa135790");
|
|
|
|
|
MailUtil mailUtil = new MailUtil();
|
|
|
|
|
mailUtil.init(mailConfig);
|
|
|
|
|
mailUtil.setNick("yunhao");
|
|
|
|
|
mailUtil.setTo("yunhaok@163.com");
|
|
|
|
|
mailUtil.setContentType(ImppEnumUtil.MESSAGE_TYPE_CONTENT.TEXT.getDescription());
|
|
|
|
|
mailUtil.setSubject("test");
|
|
|
|
|
mailUtil.setBody("666");
|
|
|
|
|
File file = new File("C:\\Users\\yunha\\Desktop\\miss_file (2).txt");
|
|
|
|
|
mailUtil.addAttachment(file);
|
|
|
|
|
mailUtil.send();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|