com.takaitra.util
Class VelocityEmail

java.lang.Object
  extended by org.apache.commons.mail.Email
      extended by org.apache.commons.mail.MultiPartEmail
          extended by org.apache.commons.mail.HtmlEmail
              extended by com.takaitra.util.VelocityEmail

public class VelocityEmail
extends org.apache.commons.mail.HtmlEmail

Extends the HtmlEmail class from Apache Commons. Allows the content of the email to be easily created via the specified Velocity template and POJO.

Prerequisites

These instructions work well in a J2EE environment. Feel free to tweak in order to fit your own needs.

Example Usage

 // Initialize the VelocityEmail object
 VelocityEmail email = new VelocityEmail(templateName);
 email.setHostName(smtphost);
 email.setFrom(fromAddress);
 email.setTo(toAddress);
 
 // Merge a single javabean with the template. It will be referenced as in
 // $bean.fieldname in your template. 
 email.setHtmlMsg(javabean)
 
 // Alternatively, you may create your own VelocityContext to merge multiple
 // javabeans with your template.
 // VelocityContext context = new VelocityContext();
 // context.put("bean1", javabean1);
 // context.put("bean2", javabean2);
 // email.setHtmlMsg(context);
 
 // Send the email
 email.send();
 
 

Version:
1.0
Author:
Matthew Toso

Field Summary
 
Fields inherited from class org.apache.commons.mail.HtmlEmail
CID_LENGTH
 
Fields inherited from class org.apache.commons.mail.Email
ATTACHMENTS, CONTENT_TYPE, EMAIL_BODY, EMAIL_SUBJECT, FILE_SERVER, ISO_8859_1, KOI8_R, MAIL_DEBUG, MAIL_HOST, MAIL_PORT, MAIL_SMTP_AUTH, MAIL_SMTP_CONNECTIONTIMEOUT, MAIL_SMTP_FROM, MAIL_SMTP_PASSWORD, MAIL_SMTP_SOCKET_FACTORY_CLASS, MAIL_SMTP_SOCKET_FACTORY_FALLBACK, MAIL_SMTP_SOCKET_FACTORY_PORT, MAIL_SMTP_TIMEOUT, MAIL_SMTP_USER, MAIL_TRANSPORT_PROTOCOL, MAIL_TRANSPORT_TLS, RECEIVER_EMAIL, RECEIVER_NAME, SENDER_EMAIL, SENDER_NAME, SMTP, TEXT_HTML, TEXT_PLAIN, US_ASCII
 
Constructor Summary
VelocityEmail(java.lang.String templateFile)
          The VelocityEmail constructor.
 
Method Summary
 java.lang.String getTemplateFile()
          Return the name of the Velocity template file
 void setHtmlMsg(java.lang.Object bean)
          Set the HTML content of the email, merging the template file with the provided javabean.
 void setHtmlMsg(org.apache.velocity.VelocityContext context)
          Set the HTML content of the email, merging the template file with the provided context.
 void setTemplateFile(java.lang.String templateFile)
          Set the name of the Velocity template file
 void setTextMsg(java.lang.Object bean)
          Set the text content of the email, merging the template file with the provided javabean.
 void setTextMsg(org.apache.velocity.VelocityContext context)
          Set the text content of the email, merging the template file with the provided context.
 
Methods inherited from class org.apache.commons.mail.HtmlEmail
buildMimeMessage, embed, embed, embed, embed, embed, embed, setHtmlMsg, setMsg, setTextMsg
 
Methods inherited from class org.apache.commons.mail.MultiPartEmail
addPart, addPart, addPart, attach, attach, attach, attach, attach, getSubType, isBoolHasAttachments, setBoolHasAttachments, setSubType
 
Methods inherited from class org.apache.commons.mail.Email
addBcc, addBcc, addBcc, addCc, addCc, addCc, addHeader, addReplyTo, addReplyTo, addReplyTo, addTo, addTo, addTo, getBccAddresses, getCcAddresses, getFromAddress, getHostName, getMailSession, getMimeMessage, getReplyToAddresses, getSentDate, getSmtpPort, getSocketConnectionTimeout, getSocketTimeout, getSslSmtpPort, getSubject, getToAddresses, isSSL, isTLS, send, sendMimeMessage, setAuthentication, setAuthenticator, setBcc, setBounceAddress, setCc, setCharset, setContent, setContent, setDebug, setFrom, setFrom, setFrom, setHeaders, setHostName, setMailSession, setMailSessionFromJNDI, setPopBeforeSmtp, setReplyTo, setSentDate, setSmtpPort, setSocketConnectionTimeout, setSocketTimeout, setSSL, setSslSmtpPort, setSubject, setTLS, setTo, updateContentType
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VelocityEmail

public VelocityEmail(java.lang.String templateFile)
The VelocityEmail constructor.

Parameters:
templateFile - the Velocity template file name
Method Detail

setHtmlMsg

public void setHtmlMsg(java.lang.Object bean)
                throws org.apache.commons.mail.EmailException
Set the HTML content of the email, merging the template file with the provided javabean. The javabean will be called "bean" in the context.

Parameters:
bean - The javabean to be merged
Throws:
org.apache.commons.mail.EmailException

setHtmlMsg

public void setHtmlMsg(org.apache.velocity.VelocityContext context)
                throws org.apache.commons.mail.EmailException
Set the HTML content of the email, merging the template file with the provided context. Allows for multiple objects in the context.

Parameters:
context - The Velocity context to be merged
Throws:
org.apache.commons.mail.EmailException

setTextMsg

public void setTextMsg(java.lang.Object bean)
                throws org.apache.commons.mail.EmailException
Set the text content of the email, merging the template file with the provided javabean. The javabean will be called "bean" in the context.

Parameters:
bean - The javabean to be merged
Throws:
org.apache.commons.mail.EmailException

setTextMsg

public void setTextMsg(org.apache.velocity.VelocityContext context)
                throws org.apache.commons.mail.EmailException
Set the text content of the email, merging the template file with the provided context. Allows for multiple objects in the context.

Parameters:
context - The Velocity context to be merged
Throws:
org.apache.commons.mail.EmailException

getTemplateFile

public java.lang.String getTemplateFile()
Return the name of the Velocity template file

Returns:
the Velocity template file name

setTemplateFile

public void setTemplateFile(java.lang.String templateFile)
Set the name of the Velocity template file

Parameters:
templateFile - the Velocity template file name