importorg.apache.commons.lang3.ArrayUtils;importorg.springframework.core.task.TaskExecutor;importorg.springframework.mail.javamail.JavaMailSender;importorg.springframework.mail.javamail.MimeMessageHelper;importorg.springframework.stereotype.Service;importjavax.annotation.Resource;importjavax.mail.MessagingException;importjavax.mail.internet.MimeMessage;/***
* MailServiceImpl
*
* Description:
* 邮件系统实现类
*@authorjianzh5
*@version2017/3/31 17:10
*@since1.0*/@Servicepublic class MailServiceImpl implementsIMailService{private LoggerUtil logger = LoggerUtil.getLogger(this.getClass());
@ResourceprivateTaskExecutor taskExecutor;
@ResourceprivateJavaMailSender javaMailSender;
@Overridepublic voidsendMail(MailEntry mailEntry) {
MimeMessage message=javaMailSender.createMimeMessage();try{
MimeMessageHelper helper= new MimeMessageHelper(message, true, "utf-8");
helper.setFrom("xxx@company.com");
helper.setTo(mailEntry.getRecipients());//收件人
if(ArrayUtils.isNotEmpty(mailEntry.getCarbonCopy())){
helper.setCc(mailEntry.getCarbonCopy());//抄送人
}
helper.setSubject(mailEntry.getSubject());
helper.setText(mailEntry.getText(),true);//设置为TRUE则可以使用Html标记
addSendMailTask(message);
}catch(MessagingException e) {//e.printStackTrace();
logger.error("邮件转换异常,邮件详细信息为{}",e.getMessage());
}
}/*** @desc 使用多线程发送邮件
*@authorjianzh5
* @date 2017/4/1 11:41
*@parammessage MimeMessage邮件封装类*/
private void addSendMailTask(finalMimeMessage message){try{
taskExecutor.execute(newRunnable() {
@Overridepublic voidrun() {
javaMailSender.send(message);
}
});
}catch(Exception e){
logger.error("邮件发送异常,邮件详细信息为{}",e.getMessage());
}
}
}