Mentawai Web Framework

Sending Email

You probably want to send a welcome email message to your users when they register in your site, right? Mentawai offers you an easy way to send email messages through the excellent Jakarta Commons Email. The code of Commons Email is incorporated into the Mentawai code (inside the package org.mentawai.mail), so that you don't need to install anything.

Unfortunetly, to send email from Java, you need two language extensions: JavaMail (javax.mail.*) and JAF (javax.activation.*). Mentawai comes with the jar files for these extensions inside its /lib directory, so all you have to do is grab mail.jar and activation.jar from there and place them inside your application's /WEB-INF/lib directory.

Let's see an example:

import org.mentawai.core.*;
import org.mentawai.mail.*;

public class ApplicationManager extends org.mentawai.core.ApplicationManager {
    
    public void init() {
        Email.setDefaultAuthentication("cadastro@mentawai.com.br", "whatever");
        Email.setDefaultHostName("smtp.mentawai.com.br");
        Email.setDefaultFrom("cadastro@mentawai.com.br", "Mentawai");
    }
	
    public void loadActions() {
        // ...
    }
}
        

We can use the init() method of our ApplicationManager to setup the default values for the smtp hostname and authentication(if needed). We can also setup a default sender email and name.

Now to send an email message from an action, you have to do this:

import org.mentawai.core.*;
import org.mentawai.mail.*;

public class HelloEmail extends BaseAction {
	
	public String execute() throws Exception {
        try {
            SimpleEmail email = new SimpleEmail();
            email.addTo("soliveira@smartjava.com.br", "Sergio Oliveira");
            email.setSubject("Testing...");
            email.setMsg("Hi,\n\nBye!");
            email.send();            
        } catch(Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
	}
}
        

There is also support for HTML email messages (org.mentawai.mail.HtmlMail) and attachments (org.mentawai.mail.MultiPartEmail). Please refer to the API documentation for more details on these classes.


Using localized email templates

You can use the org.mentawai.mail.Letter interface along with one of its implementations to send localized email messages. The org.mentawai.mail.VelocityLetter that comes with Mentawai, allows you to process velocity templates to generate email messages.

To use Velocity you must place the velocity jar file inside your application's /WEB-INF/lib directory. You can find the file velocity-dep-1.4.jar inside Mentawai's /lib directory.

Check the example below:

        try {
            Letter letter = new VelocityLetter("hello.html"); // default dir is /letters
            letter.setAttribute("name", "Sergio");
            letter.setAttribute("age", "29");
            String msg = letter.getText(loc); // loc comes from action
            
            HtmlEmail email = new HtmlEmail();
            email.addTo("soliveira@mentawai.com.br", "Sergio Oliveira");
            email.setSubject("Testing...");
            email.setHtmlMsg(msg);
            email.send(); 
        } catch(Exception e) {
            e.printStackTrace();
            throw new ActionException(e);
        }        
        

Notice that the class VelocityLetter looks for files in the /letters directory by default. You can change that by passing a new directory in the constructor. (Check the API documentation for more details)

In the above example, if the user locale is en_US, the template file will be /letters/en_US/hello.html. Notice that you must place your templates in the directory corresponding to their locale.