Mentawai makes it very easy to send email messages. It supports neat features like templates, sending asynchronously, internationalization and more.
Setting up in the application manager:
@Override public void init(Context application) { Props props = getProps(); if (!props.getBoolean("email.send_email")) { Email.setSendEmail(false); // no email will be sent... good for testing the web application } else { Email.setDefaultHostName(props.getString("email.host")); // the smtp host Email.setDefaultPort( props.getInt("email.port") ); // the smtp port Email.setDefaultSslConnection( props.getBoolean("email.ssl") ); // does it use SSL like gmail? if (props.getBoolean("email.use_authentication")) { // does it require authentication? Email.setDefaultAuthentication(props.getString("email.user"), props.getString("email.pass")); } // so you don't have to specify the fromEmail and toEmail every time (but you still can if you want to) Email.setDefaultFrom(props.getString("email.from_email"), props.getString("email.from_name")); } }
Properties configuration to use a gmail account:
# Email setup email.send_email = true email.host = smtp.gmail.com email.ssl = true email.port = 465 email.use_authentication = true email.user = my.gmail.username@gmail.com email.pass = abc123 email.from_email = my.gmail.username@gmail.com email.from_name = The Web Site Name Here
Now send:
// block until message was sent: SimpleEmail.sendNow("Sergio Oliveira Jr.", "sergio@sergio.com", "Email test from Mentawai!", "Hi there,\n\nThis is a test!\n\nBye!\n"); // return immediately and send on another thread asynchronously (you will assume it will NOT fail and if it fails you don't care) SimpleEmail.sendLater("Sergio Oliveira Jr.", "sergio@sergio.com", "Email test from Mentawai!", "Hi there,\n\nThis is a test!\n\nBye!\n"); // block until message was sent: HtmlEmail.sendNow("Sergio Oliveira Jr.", "sergio@sergio.com", "Email test from Mentawai!", "<p>Hi there,\n\nThis is a <b>test</b>!\n\nBye!\n</p>"); // return immediately and send on another thread asynchronously (you will assume it will NOT fail and if it fails you don't care) HtmlEmail.sendLater("Sergio Oliveira Jr.", "sergio@sergio.com", "Email test from Mentawai!", "<p>Hi there,\n\nThis is a <b>test</b>!\n\nBye!\n</p>");
Using localized templates:
User user = getSessionObj(); Letter welcome = new TextLetter("welcome.txt"); // <=== this can also be a HTML template file (welcome.html) welcome.setAttribute("username", u.getUsername()); welcome.setAttribute("password", u.getPassword()); try { String subject = welcome.getSubject(getSessionLocale()); String body = welcome.getText(getSessionLocale()); SimpleEmail.sendLater(u.getUsername(), u.getEmail(), subject, body); // <=== you can use HtmlEmail for HTML email message } catch(Exception e) { System.err.println("Error sending email to: " + u.getEmail()); e.printStackTrace(); }
The template file:
The template file goes inside letters/locale folder, in the root directory of your web application. So for example, the file letters/en_US/welcome.txt could be:
Welcome to Menta Web App Thanks for registering. Your information is below: Username: $username Password: $password Best regards, Menta Web App
NOTE: The first line is the subject line of the email message you can also localize.