1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| @Slf4j @Component public class SendMailService { @Autowired private JavaMailSenderImpl mailSender; @Autowired private SendEmailRecordDao sendEmailRecordDao; @Autowired private ShellProperties shellProperties;
public void SendMailWithAttach(SendEmailRecord sendEmailInfo){ try { log.info("start to send email to {}",sendEmailInfo.getTo()); MimeMessageHelper messageHelper = new MimeMessageHelper(mailSender.createMimeMessage(), true); sendEmailInfo.setFrom(getMailSendFrom()); messageHelper.setFrom(sendEmailInfo.getFrom()); messageHelper.setTo(sendEmailInfo.getTo().split(",")); messageHelper.setSubject(sendEmailInfo.getSubject()); messageHelper.setText(sendEmailInfo.getText()); if (StrUtil.isNotBlank(sendEmailInfo.getCc())) { messageHelper.setCc(sendEmailInfo.getCc().split(",")); } if (StrUtil.isNotBlank(sendEmailInfo.getBcc())) { messageHelper.setCc(sendEmailInfo.getBcc().split(",")); } if (CollUtil.isNotEmpty(sendEmailInfo.getFileList())) { for (File file : sendEmailInfo.getFileList()) { messageHelper.addAttachment(file.getName(),file); } } mailSender.send(messageHelper.getMimeMessage()); sendEmailInfo.setStatus(SendMailStatusEnum.OK.getCode()); log.info("Send Email Success :{}->{}", sendEmailInfo.getFrom(), sendEmailInfo.getTo()); } catch (Exception e) { log.error("send Email failed , error is {}",e.getMessage()); sendEmailInfo.setStatus(SendMailStatusEnum.FAILED.getCode()); sendEmailInfo.setError(e.getMessage()); }finally { saveMail(sendEmailInfo); } }
private void saveMail(SendEmailRecord emailInfo) { log.info("start save send email record to database."); if (Objects.equals(emailInfo.getTo(),shellProperties.getAlarmEmail())){ log.info("this is alarm email , email info is {}",emailInfo); emailInfo.setStatus(SendMailStatusEnum.FAILED.getCode()); } sendEmailRecordDao.insertSelective(emailInfo); }
public String getMailSendFrom() { return mailSender.getJavaMailProperties().getProperty("from"); } }
|