org.apache.turbine.util.velocity
Class VelocityHtmlEmail

java.lang.Object
  extended byorg.apache.commons.mail.Email
      extended byorg.apache.commons.mail.MultiPartEmail
          extended byorg.apache.commons.mail.HtmlEmail
              extended byorg.apache.turbine.util.velocity.VelocityHtmlEmail

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

This is a simple class for sending html email from within Velocity. Essentially, the bodies (text and html) of the email are a Velocity Context objects. The beauty of this is that you can send email from within your Velocity template or from your business logic in your Java code. The body of the email is just a Velocity template so you can use all the template functionality of Velocity within your emails!

This class allows you to send HTML email with embedded content and/or with attachments. You can access the VelocityHtmlEmail instance within your templates trough the $mail Velocity variable.

VelocityHtmlEmail myEmail= new VelocityHtmlEmail(data);
context.put("mail", myMail);
or VelocityHtmlEmail myEmail= new VelocityHtmlEmail(context);
context.put("mail", myMail);

The templates should be located under your Template turbine directory.

This class wraps the HtmlEmail class from commons-email. Thus, it uses the JavaMail API and also depends on having the mail.server property set in the TurbineResources.properties file. If you want to use this class outside of Turbine for general processing that is also possible by making sure to set the path to the TurbineResources.properties. See the TurbineResourceService.setPropertiesFileName() method for more information.

This class is basically a conversion of the WebMacroHtmlEmail written by Regis Koenig

You can turn on debugging for the JavaMail API by calling setDebug(true). The debugging messages will be written to System.out.

Version:
$Id: VelocityHtmlEmail.java 659576 2008-05-23 16:00:15Z seade $
Author:
Eric Pugh, Andre Schild, Quinton McCombs, Henning P. Schmiedehausen

Field Summary
 
Fields inherited from class org.apache.commons.mail.HtmlEmail
CID_LENGTH, html, inlineImages, text
 
Fields inherited from class org.apache.commons.mail.Email
ATTACHMENTS, authenticator, bccList, bounceAddress, ccList, charset, content, CONTENT_TYPE, contentType, debug, EMAIL_BODY, EMAIL_SUBJECT, emailBody, FILE_SERVER, fromAddress, headers, hostName, ISO_8859_1, KOI8_R, MAIL_DEBUG, MAIL_HOST, MAIL_PORT, MAIL_SMTP_AUTH, MAIL_SMTP_FROM, MAIL_TRANSPORT_PROTOCOL, message, popBeforeSmtp, popHost, popPassword, popUsername, RECEIVER_EMAIL, RECEIVER_NAME, replyList, SENDER_EMAIL, SENDER_NAME, sentDate, SMTP, smtpPort, subject, TEXT_HTML, TEXT_PLAIN, toList, US_ASCII
 
Constructor Summary
VelocityHtmlEmail(org.apache.velocity.context.Context context)
          Constructor, sets the context object.
VelocityHtmlEmail(RunData data)
          Constructor, sets the context object from the passed RunData object
 
Method Summary
 java.lang.String embed(java.lang.String surl, java.lang.String name)
          Embed a file in the mail.
 java.lang.String getCid(java.lang.String filename)
          Get the cid of an embedded file.
 java.lang.String getMailServer()
          Gets the host name of the outgoing mail server.
 java.lang.String send()
          Actually send the mail.
 VelocityHtmlEmail setHtmlTemplate(java.lang.String template)
          Set the HTML template for the mail.
 void setMailServer(java.lang.String serverAddress)
          Sets the address of the outgoing mail server.
 VelocityHtmlEmail setTextTemplate(java.lang.String template)
          Set the text template for the mail.
 
Methods inherited from class org.apache.commons.mail.HtmlEmail
buildMimeMessage, embed, setHtmlMsg, setMsg, setTextMsg
 
Methods inherited from class org.apache.commons.mail.MultiPartEmail
addPart, addPart, addPart, attach, attach, attach, attach, attach, createBodyPart, createMimeMultipart, getContainer, getPrimaryBodyPart, getSubType, init, isBoolHasAttachments, isInitialized, setBoolHasAttachments, setInitialized, setSubType
 
Methods inherited from class org.apache.commons.mail.Email
addBcc, addBcc, addCc, addCc, addHeader, addReplyTo, addReplyTo, addTo, addTo, getFromAddress, getHostName, getMailSession, getMimeMessage, getSentDate, getSmtpPort, getSubject, sendMimeMessage, setAuthentication, setAuthenticator, setBcc, setBounceAddress, setCc, setCharset, setContent, setContent, setDebug, setFrom, setFrom, setHeaders, setHostName, setMailSession, setPopBeforeSmtp, setSentDate, setSmtpPort, setSubject, setTo, toInternetAddressArray
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

VelocityHtmlEmail

public VelocityHtmlEmail(RunData data)
Constructor, sets the context object from the passed RunData object

Parameters:
data - A Turbine RunData object.

VelocityHtmlEmail

public VelocityHtmlEmail(org.apache.velocity.context.Context context)
Constructor, sets the context object.

Parameters:
context - A Velocity context object.
Method Detail

setHtmlTemplate

public VelocityHtmlEmail setHtmlTemplate(java.lang.String template)
Set the HTML template for the mail. This is the Velocity template to execute for the HTML part. Path is relative to the VM templates directory.

Parameters:
template - A String.
Returns:
A VelocityHtmlEmail (self).

setTextTemplate

public VelocityHtmlEmail setTextTemplate(java.lang.String template)
Set the text template for the mail. This is the Velocity template to execute for the text part. Path is relative to the VM templates directory

Parameters:
template - A String.
Returns:
A VelocityHtmlEmail (self).

setMailServer

public void setMailServer(java.lang.String serverAddress)
Sets the address of the outgoing mail server. This method should be used when you need to override the value stored in TR.props.

Parameters:
serverAddress - host name of your outgoing mail server

getMailServer

public java.lang.String getMailServer()
Gets the host name of the outgoing mail server. If the server name has not been set by calling setMailServer(), the value from TR.props for mail.server will be returned. If TR.props has no value for mail.server, localhost will be returned.

Returns:
host name of the mail server.

send

public java.lang.String send()
                      throws org.apache.commons.mail.EmailException
Actually send the mail.

Throws:
org.apache.commons.mail.EmailException - thrown if mail cannot be sent.

embed

public java.lang.String embed(java.lang.String surl,
                              java.lang.String name)
                       throws org.apache.commons.mail.EmailException
Embed a file in the mail. The file can be referenced through its Content-ID. This function also registers the CID in an internal map, so the embedded file can be referenced more than once by using the getCid() function. This may be useful in a template.

Example of template:

 <html>
 <!-- $mail.embed("http://server/border.gif","border.gif"); -->
 <img src=$mail.getCid("border.gif")>
 <p>This is your content
 <img src=$mail.getCid("border.gif")>
 </html>
 

Parameters:
surl - A String.
name - A String.
Returns:
A String with the cid of the embedded file.
Throws:
org.apache.commons.mail.EmailException
See Also:
embed.

getCid

public java.lang.String getCid(java.lang.String filename)
Get the cid of an embedded file.

Parameters:
filename - A String.
Returns:
A String with the cid of the embedded file.
See Also:
embed.


Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.