SeamFramework.orgCommunity Documentation

Chapter 4. Templating

4.1. Basics
4.2. Velocity
4.3. FreeMarker

Instead of creating your message body as a String and setting it via the bodyText, bodyHtml and bodyHtmlTextAlt methods you can use integrated templating to substitute variables for values. Seam Mail supports templating in any of the body parts as well as the subject.

The template can either be loaded as from a java.lang.String, java.io.File or java.io.InputStream

Values to be used in place of your template variables are loaded via the put method, as a Map or optionally resolved via EL in Velocity

Example Usage. See Velocity documentation for syntax.

                

    MailMessage m = mailMessage.get();
    m.subject(new VelocityTempalte("Weather for $city, $state")
    m.bodyText(new VelocityTemplate(new File("myFile.template"));
    m.put("city", "Boston");
    m.put("state", "Massachusetts");
    m.put("now", new Date());
    

A Velocity Template can be optionally created with an CDI injected CDIVelocityContext. This adds the ability to resolve your variables via the built in velocity context and a fall through to resolve CDI beans which are @Named.



    @Inject   
    private CDIVelocityContext cdiContext;
    
    MailMessage m = mailMessage.get();
    m.bodyText(new VelocityTemplate(new File("myFile.template"), cdiContext);
    

FreeMarker usage is essentially the same as Velocity except there is no EL functionality. Consult FreeMarker documentation for Syntax

                

    MailMessage m = mailMessage.get();
    m.subject(new FreeMarkerTemplate("ATTN: ${name}")
    m.bodyText(new FreeMarkerTemplate("Hello ${name} you have won 1M USD!"));
    m.put("name", "Ed Mcmahon");