Exemplo n.º 1
0
  /**
   * 发送邮件
   *
   * @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;
  }