/** * 发送邮件到邮件队列 * * @param vo * @param taskId */ @SuppressWarnings("rawtypes") public void insertEmailQueue(MailBo vo, Integer taskId) { try { String content = freeMarkerHelper.generateContent(vo.getTemplate(), vo.getTemplateMap()); Integer emailType = vo.getEmailType() == null ? MailConstants.MAIL_TYPE_SIMPLIFIED : vo.getEmailType(); if (ArrayUtils.isNotEmpty(vo.getTo()) && StringUtils.isNotBlank(content)) { String sql = "INSERT INTO email_queue(title, from_addr, to_addr, from_name, task_id, content, email_type, create_date)" + " VALUES(?, ?, ?, ?, ?, ?, ?, ?)"; jdbcTemplateCount.update( sql, vo.getSubject(), vo.getFrom(), StringUtils.join(vo.getTo(), MailConstants.MAIL_ADDRESS_SEPARATE_CHAR), vo.getFromName(), taskId, content, emailType, new Date()); } } catch (DataAccessException e) { e.printStackTrace(); logger.error(" === INSEET DATA TO email_queue ON ERROR! === "); } }
/** * 发送邮件 * * @param vo 邮件信息 * @return */ public boolean send(MailBo vo) { boolean isSuccess = false; JavaMailSender sender = getMailSenderByVo(vo); MimeMessage message = sender.createMimeMessage(); MimeMessageHelper helper; String fromName = vo.getFromName(); String fromAddr = vo.getFrom(); String subject = vo.getSubject(); Integer emailType = vo.getEmailType() == null ? MailConstants.MAIL_TYPE_SIMPLIFIED : vo.getEmailType(); String content = freeMarkerHelper.generateContent(vo.getTemplate(), vo.getTemplateMap()); if (StringUtils.endsWith(fromAddr, "@job5156.com")) { // 已备案通道,取消伪装发送 2014-10-16 // fromAddr = "chitone" + RandomStringUtils.randomAlphanumeric(4) + "@job5156.com"; } if (emailType.equals(MailConstants.MAIL_TYPE_TRADITIONAL)) { /*fromName = FontConvert.gbkToBig5(fromName); subject = FontConvert.gbkToBig5(subject); content = FontConvert.gbkToBig5(content);*/ fromName = ChineseCodeChangeUtil.toTraditional(fromName); subject = ChineseCodeChangeUtil.toTraditional(subject); content = ChineseCodeChangeUtil.toTraditional(content); } try { helper = new MimeMessageHelper(message, true, "UTF-8"); helper.setFrom(fromAddr, fromName); helper.setTo(vo.getTo()); helper.setSubject(subject); helper.setText(content, true); // 添加附件到邮件消息中 MailAttachVo[] attachList = vo.getAttachList(); if (attachList != null) { for (MailAttachVo attach : attachList) { if (attach.getFile() == null) continue; helper.addAttachment(attach.getAttachName(), attach.getFile()); } } // helper.addAttachment(); } catch (MessagingException | UnsupportedEncodingException e) { e.printStackTrace(); } try { sender.send(message); isSuccess = true; } catch (Exception ex) { isSuccess = false; ex.printStackTrace(); System.err.println(ex.getMessage()); } return isSuccess; }
@SuppressWarnings("rawtypes") private JavaMailSender getMailSenderByVo(MailBo vo) { return getMailSenderByServer(vo.getServer()); }