使用邮件发送自定义报表

使用velocity作为模板

设置velocity模板

public class VelocityTemplateFactory {

    public static void init() {
        Velocity.setProperty("resource.loader", "file");
        Velocity.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
        //Velocity.setProperty("file.resource.loader.path", Task.JOB_PATH + File.separator + "conf");
        Velocity.setProperty("file.resource.loader.path", "conf");
        Velocity.setProperty("file.resource.loader.cache", "false");
        Velocity.setProperty("file.resource.loader.modificationCheckInterval", "2");
        Velocity.setProperty("input.encoding", "UTF-8");
        Velocity.setProperty("output.encoding", "UTF-8");
        Velocity.setProperty("default.contentType", "text/html; charset=UTF-8");
        Velocity.setProperty("velocimarco.library.autoreload", "true");
        Velocity.setProperty("runtime.log.error.stacktrace", "false");
        Velocity.setProperty("runtime.log.warn.stacktrace", "false");
        Velocity.setProperty("runtime.log.info.stacktrace", "false");
        Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SimpleLog4JLogSystem");
        Velocity.setProperty("runtime.log.logsystem.log4j.category", "velocity_log");
        try {
            Velocity.init();
        } catch (Throwable e) {
            e.printStackTrace();
            System.err.println("init velocity faild to exit");
//			System.exit(0);
        }
    }
}
public class Test {
    private static final Logger logger = Logger.getLogger(Test.class);
    // 页面模板
    private static final String TEMPLATE = "/template/businessdailyreport.html";
    private static ExecutorService threadPool = Executors.newFixedThreadPool(2);
    public static CountDownLatch countDownLatch = new CountDownLatch(2);

    static {
    	// 初始化模板引擎
        VelocityTemplateFactory.init();
    }

    public static void main(String[] args) throws Exception {
        Map<String, Object> ultimateDateMap = new HashMap<String, Object>();
        // 使用线程获得html模板使用的数据放到Map中
        WeekAccumulativeThread weekAccumulativeThread = new WeekAccumulativeThread(ultimateDateMap);
        TodayAccumulativeThread todayAccumulativeThread = new TodayAccumulativeThread(ultimateDateMap);
        
        threadPool.submit(weekAccumulativeThread);
        threadPool.submit(todayAccumulativeThread);

        BusinessIncomeDailyTask.countDownLatch.await();

        logger.info("ultimateDateMap:" + ultimateDateMap);
        // 得到html模板
        Template template = Velocity.getTemplate(TEMPLATE);
        // 得到的数据填充的html模板中
        Context context = new VelocityContext(ultimateDateMap);
        StringWriter stringWriter = new StringWriter();
        template.merge(context, stringWriter);
        stringWriter.flush();
        logger.info(stringWriter.toString());
        FileInputStream fis = new FileInputStream("conf/businessDailyEmail.properties");
        Properties pro = new Properties();
        pro.load(fis);
        String emails = pro.getProperty("emails");
        String[] emailsSplit = emails.split(",");
        List<String> mailList = new ArrayList<String>();
        for (String email : emailsSplit) {
            mailList.add(email);
        }
        // 发送邮件
        EmailNoticeUtil.sendEmail(stringWriter.toString(), "招聘商业日报:" + DateUtil.getDashYesterdayFormat(), mailList);
        threadPool.shutdown();
        logger.info("========== execute success ==========");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值