Exemple #1
0
 /**
  * writes processed template to file
  *
  * @param file
  * @param template
  * @param props
  * @return
  * @throws IOException
  * @throws TemplateException
  */
 private File writeTemplateToFile(File file, Template template, Properties props)
     throws IOException, TemplateException {
   String output = FreeMarkerTemplateUtils.processTemplateIntoString(template, props);
   LOG.debug("Generated File Output: " + output);
   FileUtils.writeStringToFile(file, output);
   return file;
 }
Exemple #2
0
 /**
  * 发送模板邮件
  *
  * @param toMailAddr 收信人地址
  * @param subject email主题
  * @param templatePath 模板地址
  * @param map 模板map
  */
 public static void sendFtlMail(
     String toMailAddr, String subject, String templatePath, Map<String, Object> map) {
   Template template = null;
   Configuration freeMarkerConfig = null;
   HtmlEmail hemail = new HtmlEmail();
   try {
     hemail.setHostName(getHost(from));
     hemail.setSmtpPort(getSmtpPort(from));
     hemail.setCharset(charSet);
     hemail.addTo(toMailAddr);
     hemail.setFrom(from, fromName);
     hemail.setAuthentication(username, password);
     hemail.setSubject(subject);
     freeMarkerConfig = new Configuration();
     freeMarkerConfig.setDirectoryForTemplateLoading(new File(getFilePath()));
     // 获取模板
     template =
         freeMarkerConfig.getTemplate(getFileName(templatePath), new Locale("Zh_cn"), "UTF-8");
     // 模板内容转换为string
     String htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
     System.out.println(htmlText);
     hemail.setMsg(htmlText);
     hemail.send();
     System.out.println("email send true!");
   } catch (Exception e) {
     e.printStackTrace();
     System.out.println("email send error!");
   }
 }
  @Override
  public void sendPasswordResetLink(final AppUser user) {
    try {
      if (user.getPasswordResetToken() == null || user.getPasswordResetToken().isEmpty()) {
        return;
      }
      Template template = configuration.getTemplate("resetpassword.ftl");
      final Map model = new HashMap();
      model.put("name", user.getName());
      String password_reset_url = RESET_PASSWORD_URL + "?token=" + user.getPasswordResetToken();
      model.put("password_reset_url", password_reset_url);
      final String mailBody = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
      MimeMessagePreparator preparator =
          new MimeMessagePreparator() {

            public void prepare(MimeMessage mimeMessage) throws Exception {
              MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
              message.setTo(user.getEmail());
              message.setSubject("GWA-Portal password reset confirmation");
              message.setFrom(FROM);
              message.setText(mailBody, false);
            }
          };
      mailSender.send(preparator);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * @param tpl
   * @param params
   * @return
   * @throws Exception
   */
  private String getHtmlTextFromTemplate(String tpl, Map<String, Object> params) throws Exception {

    // 通过指定模板名获取FreeMarker模板实例
    Template fm = freeMarker.getConfiguration().getTemplate(tpl);

    // FreeMarker通过Map传递动态数据
    return FreeMarkerTemplateUtils.processTemplateIntoString(fm, params);
  }
  public static String process(Map<String, Object> dataModel, String templateName)
      throws Exception {

    if (cfg == null) {
      init();
    }
    t = cfg.getTemplate(templateName);
    return FreeMarkerTemplateUtils.processTemplateIntoString(t, dataModel);
  }
 protected String buildText(JobExecution job) {
   Map<String, Object> result = getExecutionResult(job);
   try {
     fmConfg.setTemplateLoaderPath(config.getProperty("batch.freemarkDir"));
     Template t = fmConfg.getConfiguration().getTemplate(messageTemplate);
     return FreeMarkerTemplateUtils.processTemplateIntoString(t, result);
   } catch (Exception e) {
     logger.error(e.getMessage(), e);
   }
   return new StringBuffer("<html><body>")
       .append(result.toString().replace(",", ",<br/>"))
       .append("</body></html>")
       .toString();
 }
  /** 使用Freemarker生成html格式内容. */
  private String generateContent(String userName) throws MessagingException {

    try {
      @SuppressWarnings("rawtypes")
      Map context = Collections.singletonMap("userName", userName);
      return FreeMarkerTemplateUtils.processTemplateIntoString(template, context);
    } catch (IOException e) {
      logger.error("生成邮件内容失败, FreeMarker模板不存在", e);
      throw new MessagingException("FreeMarker模板不存在", e);
    } catch (TemplateException e) {
      logger.error("生成邮件内容失败, FreeMarker处理失败", e);
      throw new MessagingException("FreeMarker处理失败", e);
    }
  }
Exemple #8
0
  private String createMailTemplate(String fileName, Map<String, Object> params) {
    Template template;
    try {
      template = configuration.getTemplate(fileName);
    } catch (IOException e) {
      log.error(e.toString());
      return null;
    }

    try {
      String result = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
      log.debug("mail auth body : {}", result);
      return result;
    } catch (Exception e) {
      log.error(e.toString());
      return null;
    }
  }
Exemple #9
0
 public static String getHtmlText(String templatePath, Map<String, Object> map) {
   Template template = null;
   String htmlText = "";
   try {
     Configuration freeMarkerConfig = null;
     freeMarkerConfig = new Configuration();
     freeMarkerConfig.setDirectoryForTemplateLoading(new File(getFilePath()));
     // 获取模板
     template =
         freeMarkerConfig.getTemplate(getFileName(templatePath), new Locale("Zh_cn"), "UTF-8");
     // 模板内容转换为string
     htmlText = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
     System.out.println(htmlText);
   } catch (Exception e) {
     e.printStackTrace();
   }
   return htmlText;
 }
Exemple #10
0
 @Override
 public void sendTextMailWithClasspathTemplate(
     final MailMetaData mailMetaData,
     final String templateLocation,
     final MailContentModel model) {
   LOGGER.info("Entering method sendTextMailWithClasspathTemplate..");
   model.add("mailMeta", mailMetaData);
   try {
     final String result =
         FreeMarkerTemplateUtils.processTemplateIntoString(
             freemarkerMailConfiguration.getTemplate(templateLocation), model.getModel());
     sendTextMail(mailMetaData, result);
   } catch (Exception e) {
     throw new SendMailException(
         new StringBuilder()
             .append("Error sending mail: ")
             .append(mailMetaData.toString())
             .toString(),
         e);
   }
   LOGGER.info("Exiting method sendTextMailWithClasspathTemplate..");
 }
Exemple #11
0
  @Override
  public void sendPasswordChanged(final AppUser user) {
    try {
      Template template = configuration.getTemplate("passwordchanged.ftl");
      final Map model = new HashMap();
      model.put("name", user.getName());
      final String mailBody = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
      MimeMessagePreparator preparator =
          new MimeMessagePreparator() {

            public void prepare(MimeMessage mimeMessage) throws Exception {
              MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
              message.setTo(user.getEmail());
              message.setSubject("GWA-Portal password changed");
              message.setFrom(FROM);
              message.setText(mailBody, false);
            }
          };
      mailSender.send(preparator);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }