org.apache.turbine.util.velocity
Class VelocityHtmlEmail

java.lang.Object
  |
  +--org.apache.turbine.util.mail.Email
        |
        +--org.apache.turbine.util.mail.MultiPartEmail
              |
              +--org.apache.turbine.util.mail.HtmlEmail
                    |
                    +--org.apache.turbine.util.velocity.VelocityHtmlEmail

public class VelocityHtmlEmail
extends 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", theMessage);

The templates should be located under your Template turbine directory.

This class extends the HtmlEmail class. 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

Version:
$Id: VelocityHtmlEmail.java,v 1.1.1.1 2001/08/16 05:10:04 jvanzyl Exp $
Author:
Andre Schild

Field Summary
private  RunData data
          The cached rundata object.
private  java.util.Hashtable embmap
          The map of embedded files.
private  java.lang.String htmlTemplate
          The html template to process, relative to VM's template directory.
private  java.lang.String textTemplate
          The text template to process, relative to VM's template directory.
 
Fields inherited from class org.apache.turbine.util.mail.HtmlEmail
html, htmlContent, text
 
Fields inherited from class org.apache.turbine.util.mail.MultiPartEmail
emailBody, fileServer, main
 
Fields inherited from class org.apache.turbine.util.mail.Email
ATTACHMENTS, bccList, ccList, charset, CONTENT_TYPE, EMAIL_BODY, EMAIL_SUBJECT, FILE_SERVER, ISO_8859_1, KOI8_R, MAIL_HOST, MAIL_SERVER, MAIL_SMTP_FROM, MAIL_TRANSPORT_PROTOCOL, message, RECEIVER_EMAIL, RECEIVER_NAME, replyList, SENDER_EMAIL, SENDER_NAME, SMTP, TEXT_HTML, TEXT_PLAIN, toList, US_ASCII
 
Constructor Summary
VelocityHtmlEmail(RunData data)
          Constructor, sets the 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.
private static org.apache.velocity.context.Context getContext(RunData data)
          Return the Context needed by Velocity.
 void send()
          Actually send the mail.
 VelocityHtmlEmail setHtmlTemplate(java.lang.String template)
          Set the HTML template for the mail.
 VelocityHtmlEmail setTextTemplate(java.lang.String template)
          Set the text template for the mail.
 
Methods inherited from class org.apache.turbine.util.mail.HtmlEmail
embed, getHtmlContent, setHtmlMsg, setHtmlMsg, setMsg, setTextMsg
 
Methods inherited from class org.apache.turbine.util.mail.MultiPartEmail
attach, attach, attach, attach, attach, init, initCriteria
 
Methods inherited from class org.apache.turbine.util.mail.Email
addBcc, addCc, addReplyTo, addTo, getMailSession, initialize, setCharset, setFrom, setSentDate, setSubject, toInternetAddressArray
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

htmlTemplate

private java.lang.String htmlTemplate
The html template to process, relative to VM's template directory.

textTemplate

private java.lang.String textTemplate
The text template to process, relative to VM's template directory.

data

private RunData data
The cached rundata object.

embmap

private java.util.Hashtable embmap
The map of embedded files.
Constructor Detail

VelocityHtmlEmail

public VelocityHtmlEmail(RunData data)
                  throws javax.mail.MessagingException
Constructor, sets the RunData object.
Parameters:
data - A Turbine RunData object.
Throws:
MessagingException. -  
Method Detail

setHtmlTemplate

public VelocityHtmlEmail setHtmlTemplate(java.lang.String template)
Set the HTML template for the mail. This is the Webmacro 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).

send

public void send()
          throws javax.mail.MessagingException
Actually send the mail.
Overrides:
send in class HtmlEmail
Throws:
MessagingException. -  

embed

public java.lang.String embed(java.lang.String surl,
                              java.lang.String name)
                       throws javax.mail.MessagingException
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:
MessagingException. -  
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.

getContext

private static final org.apache.velocity.context.Context getContext(RunData data)
Return the Context needed by Velocity.
Parameters:
data - A Turbine RunData object.
Returns:
A Context.


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