package utils.mails;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.annotation.Resource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;
/**
* 类MailsServiceImpl.java的实现描述:邮件操作工具实现类
*
*
*/
public class MailsServiceImpl implements MailsService {
private final Log log = LogFactory.getLog(this.getClass());
@Resource
private JavaMailSender mailSender;
/*
* (non-Javadoc)
* @see utils.mails.MailsService#sendMimeMessage(java.util.List, java.lang.String, java.lang.String,
* java.lang.String, java.util.List)
*/
@Override
public void sendMimeMessage(final List<String> toAddressList, final String fromAddress, final String subject,
final String content, final List<String> fileList, final String contentType)
throws Exception {
try {
// 扩展信息介质接口,实现接口中的方法
MimeMessagePreparator mimeMail = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws MessagingException, UnsupportedEncodingException {
// 设置收信人地址
if (CollectionUtils.isNotEmpty(toAddressList)) {
for (String toAddress : toAddressList) {
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
}
} else {
return;
}
// 设置发件人地址
if (StringUtils.isNotBlank(fromAddress)) {
mimeMessage.setFrom(new InternetAddress(fromAddress));
} else {
return;
}
// 设置邮件主题
if (StringUtils.isNotBlank(subject)) {
mimeMessage.setSubject(subject);
}
// 多部件的,可以看做一个邮件容器,包含正文、附件等
Multipart multipart = new MimeMultipart();
// 添加正文
MimeBodyPart contentMimeBodyPart = new MimeBodyPart();
if (StringUtils.isNotBlank(content)) {
if (StringUtils.isNotBlank(contentType)) {
// contentMimeBodyPart.setContent(content, "text/html;charset=utf-8");
contentMimeBodyPart.setContent(content, contentType);
} else {
contentMimeBodyPart.setText(content);
}
}
multipart.addBodyPart(contentMimeBodyPart);
// 添加附件,可以添加多个附件
if (CollectionUtils.isNotEmpty(fileList)) {
for (String file : fileList) {
MimeBodyPart fileMimeBodyPart = new MimeBodyPart();
// 文件数据源
FileDataSource fds = new FileDataSource(file);
// 数据处理器
fileMimeBodyPart.setDataHandler(new DataHandler(fds));
// 设置文件名
fileMimeBodyPart.setFileName(MimeUtility.encodeText(fds.getName()));
multipart.addBodyPart(fileMimeBodyPart);
}
}
// 将Multipart添加MimeMessage
mimeMessage.setContent(multipart);
mimeMessage.setSentDate(new Date());
}
};
// 发送邮件
mailSender.send(mimeMail);
} catch (Exception e) {
log.error("toAddress:" + toAddressList.toString() + ",fromAddress:" + fromAddress + ",send mail fail:", e);
throw new Exception("send mail fail");
}
}
}