Skip to content

Developing new email

Template Email

In our example we are going to implement the send of a birthday email.

For that, we need to implement 3 steps.

First Step - Mail Pojo.

In this step we need to create a class and mark as @MailPojo.

Example:

package meceap.misy101.pojo;

import br.com.me.ceap.dynamic.extension.model.base.MailPojo;

@MailPojo
public class HappyBirthdayMail {
// ------------------------------ FIELDS ------------------------------

    private String name;

// --------------------- GETTER / SETTER METHODS ---------------------

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Second Step - Template.

Here we need to inform the parameters and the locale messages.

Be aware that the key must be the same name of the pojo class name. and the value must be the full name of the class.

You must to save the template as a mapping file src/main/mapping/templatemail.

Example: src/main/mapping/templatemail/HappyBirthday.tmm.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:template-mail-mapping xmlns:ns2="http://meceap.me.com.br/schema/tm-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://meceap.me.com.br/schema/tm-mapping http://docs.miisy.me/xsd/tm-mapping.xsd">
    <template-mail name="HappyBirthday" type="Custom" master="false" masterTemplate="DefaultMasterTemplate" updatable="true">
        <parameters>
            <entry>
                <key>happybirthdaymail</key>
                <value>meceap.misy101.pojo.HappyBirthdayMail</value>
            </entry>
        </parameters>
        <template-locale-settings locale="en">
            <subject><![CDATA[Happy Birthday]]></subject>
            <text>
                <![CDATA[
                    Hello, $happybirthdaymail.name happy birthday!
                ]]>
            </text>
        </template-locale-settings>
        <template-locale-settings locale="pt">
            <subject><![CDATA[Feliz Aniversario]]></subject>
            <text>
                <![CDATA[
                    Olá, $happybirthdaymail.name feliz aniversario!
                ]]>
            </text>
        </template-locale-settings>
    </template-mail>
</ns2:template-mail-mapping>

Third Step - Dispatcher.

For that we must to use the ExposedMailManager.

As you can see, the manager exposes a method named sendCustomMail that receives three arguments, the template name, the mail options and the pojo instance.

Example:

@Service
public class BirthdayService {

    @Inject
    private ExposedMailManager exposedMailManager;

    public void sendEmail(UserHumanBeing humanBeing) {
        MailOptions options = new MailOptions();
        options.addRecipient("admin@misy.com");
        options.setMeLocale(MELocale.pt);
        options.setFrom(exposedMailManager.getSystemFromMail());

        HappyBirthdayMail pojo = new HappyBirthdayMail();
        pojo.setName(humanBeing.getName());

        exposedMailManager.sendCustomMail("HappyBirthday", options, pojo);
    }   
}