简体   繁体   中英

How to pass freemarker email template as email in Liferay?

I have used Freemarker for creating a template which I will be using to send as email. Here is the snippet of the parameters that i wish to include in the template. Iam using java..

//use freemarker Configuration config = new Configuration(); config.setTemplateLoader(new ClassTemplateLoader(getClass(), "/"));

        Template template = config.getTemplate("helloworld.ftl");

        // Build the data-model
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("message", "Hello!! You have got a new approval mail!");

        //List parsing 
        List<String> mailDetails = new ArrayList<String>();
        mailDetails.add(fromAddress);
        mailDetails.add(fromName);
        mailDetails.add(toAddress);
        mailDetails.add(toName);
        mailDetails.add(subject);
        mailDetails.add(body);

        data.put("mailDetails", mailDetails);


        // Console output
        Writer out = new OutputStreamWriter(System.out);
        template.process(data, out);
        out.flush();

This is tested and it successfully created a template in the specified folder. All I want to know is how do i pass the template that is generated as a parameter while sending mail?

I am sending email as follows in Liferay How should I pass the template as a parameter while sending mail?

You're writing to System.out

    // Console output
    Writer out = new OutputStreamWriter(System.out);
    template.process(data, out);
    out.flush();

You can write to a String:

    StringWriter out = new StringWriter();
    template.process(data, out);
    String finishedMessage = out.toString();

or pass any other writer to the process() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM